Skip to content

Commit a267927

Browse files
committed
Clean up and update to modern code conventions
Add some typehints for returns, and parameters of internal methods, where we *know* the correct type is always used. Also use some constants. Should be a no-op Change-Id: I6880043787fe9dd38db4f0a00bdea8f32d53e8fa
1 parent d851fbe commit a267927

File tree

5 files changed

+35
-28
lines changed

5 files changed

+35
-28
lines changed

src/Converter.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class Converter {
5858
*
5959
* @var array
6060
*/
61-
private static $precedence = [
61+
private const PRECEDENCE_LEVELS = [
6262
'or' => 2,
6363
'and' => 3,
6464
'is' => 4,
@@ -99,7 +99,7 @@ class Converter {
9999
* @param string $rule The rule to convert
100100
* @return string The RPN representation of the rule
101101
*/
102-
public static function convert( $rule ) {
102+
public static function convert( $rule ) : string {
103103
$parser = new self( $rule );
104104

105105
return $parser->doConvert();
@@ -109,7 +109,7 @@ public static function convert( $rule ) {
109109
* Private constructor.
110110
* @param string $rule
111111
*/
112-
protected function __construct( $rule ) {
112+
protected function __construct( string $rule ) {
113113
$this->rule = $rule;
114114
$this->pos = 0;
115115
$this->end = strlen( $rule );
@@ -120,7 +120,7 @@ protected function __construct( $rule ) {
120120
*
121121
* @return string The RPN representation of the rule (e.g. "5 3 mod n is")
122122
*/
123-
protected function doConvert() {
123+
protected function doConvert() : string {
124124
$expectOperator = true;
125125

126126
// Iterate through all tokens, saving the operators and operands to a
@@ -146,8 +146,10 @@ protected function doConvert() {
146146
// Resolve higher precedence levels
147147
/** @var Operator $lastOp */
148148
$lastOp = end( $this->operators );
149-
// @phan-suppress-next-line PhanUndeclaredProperty
150-
while ( $lastOp && self::$precedence[$token->name] <= self::$precedence[$lastOp->name] ) {
149+
// @phan-suppress-next-next-line PhanUndeclaredProperty
150+
while ( $lastOp &&
151+
self::PRECEDENCE_LEVELS[$token->name] <= self::PRECEDENCE_LEVELS[$lastOp->name]
152+
) {
151153
$this->doOperation( $lastOp );
152154
array_pop( $this->operators );
153155
$lastOp = end( $this->operators );
@@ -245,7 +247,7 @@ protected function nextToken() {
245247
// Two-word operators like "is not" take precedence over single-word operators like "is"
246248
if ( $word2 !== '' ) {
247249
$bothWords = "{$word1}-{$word2}";
248-
if ( isset( self::$precedence[$bothWords] ) ) {
250+
if ( isset( self::PRECEDENCE_LEVELS[$bothWords] ) ) {
249251
$token = $this->newOperator( $bothWords, $this->pos, $nextTokenPos - $this->pos );
250252
$this->pos = $nextTokenPos;
251253

@@ -254,7 +256,7 @@ protected function nextToken() {
254256
}
255257

256258
// Single-word operators
257-
if ( isset( self::$precedence[$word1] ) ) {
259+
if ( isset( self::PRECEDENCE_LEVELS[$word1] ) ) {
258260
$token = $this->newOperator( $word1, $this->pos, strlen( $word1 ) );
259261
$this->pos += strlen( $word1 );
260262

@@ -306,7 +308,7 @@ protected function doOperation( Operator $op ) {
306308
* @param int $pos
307309
* @return Expression The numerical expression
308310
*/
309-
protected function newNumber( $text, $pos ) {
311+
protected function newNumber( string $text, int $pos ) : Expression {
310312
return new Expression( $this, 'number', $text, $pos, strlen( $text ) );
311313
}
312314

@@ -318,15 +320,16 @@ protected function newNumber( $text, $pos ) {
318320
* @param int $length
319321
* @return Operator The operator
320322
*/
321-
protected function newOperator( $type, $pos, $length ) {
323+
protected function newOperator( string $type, int $pos, int $length ) : Operator {
322324
return new Operator( $this, $type, $pos, $length );
323325
}
324326

325327
/**
326328
* Throw an error
327329
* @param string $message
330+
* @throws Error
328331
*/
329-
protected function error( $message ) {
332+
protected function error( string $message ) {
330333
throw new Error( $message );
331334
}
332335
}

src/Converter/Expression.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function __construct( Converter $parser, $type, $rpn, $pos, $length ) {
3939
* @param string $type
4040
* @return bool
4141
*/
42-
public function isType( $type ) {
42+
public function isType( $type ) : bool {
4343
if ( $type === 'range' && ( $this->type === 'range' || $this->type === 'number' ) ) {
4444
return true;
4545
}

src/Converter/Operator.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Operator extends Fragment {
2929
*
3030
* @var array
3131
*/
32-
private static $opTypes = [
32+
private const OP_TYPES = [
3333
'or' => 'bbb',
3434
'and' => 'bbb',
3535
'is' => 'nnb',
@@ -48,7 +48,7 @@ class Operator extends Fragment {
4848
*
4949
* @var array
5050
*/
51-
private static $typeSpecMap = [
51+
private const TYPE_SPEC_MAP = [
5252
'b' => 'boolean',
5353
'n' => 'number',
5454
'r' => 'range',
@@ -59,7 +59,7 @@ class Operator extends Fragment {
5959
*
6060
* @var array
6161
*/
62-
private static $aliasMap = [
62+
private const ALIAS_MAP = [
6363
'%' => 'mod',
6464
'!=' => 'not-in',
6565
'=' => 'in'
@@ -75,8 +75,8 @@ class Operator extends Fragment {
7575
*/
7676
public function __construct( Converter $parser, $name, $pos, $length ) {
7777
parent::__construct( $parser, $pos, $length );
78-
if ( isset( self::$aliasMap[$name] ) ) {
79-
$name = self::$aliasMap[$name];
78+
if ( isset( self::ALIAS_MAP[$name] ) ) {
79+
$name = self::ALIAS_MAP[$name];
8080
}
8181
$this->name = $name;
8282
}
@@ -88,20 +88,24 @@ public function __construct( Converter $parser, $name, $pos, $length ) {
8888
* @param Expression $right The right part of the expression
8989
* @return Expression The result of the operation
9090
*/
91-
public function operate( Expression $left, Expression $right ) {
92-
$typeSpec = self::$opTypes[$this->name];
91+
public function operate( Expression $left, Expression $right ) : Expression {
92+
$typeSpec = self::OP_TYPES[$this->name];
9393

94-
$leftType = self::$typeSpecMap[$typeSpec[0]];
95-
$rightType = self::$typeSpecMap[$typeSpec[1]];
96-
$resultType = self::$typeSpecMap[$typeSpec[2]];
94+
$leftType = self::TYPE_SPEC_MAP[$typeSpec[0]];
95+
$rightType = self::TYPE_SPEC_MAP[$typeSpec[1]];
96+
$resultType = self::TYPE_SPEC_MAP[$typeSpec[2]];
9797

9898
$start = min( $this->pos, $left->pos, $right->pos );
9999
$end = max( $this->end, $left->end, $right->end );
100100
$length = $end - $start;
101101

102-
$newExpr = new Expression( $this->parser, $resultType,
102+
$newExpr = new Expression(
103+
$this->parser,
104+
$resultType,
103105
"{$left->rpn} {$right->rpn} {$this->name}",
104-
$start, $length );
106+
$start,
107+
$length
108+
);
105109

106110
if ( !$left->isType( $leftType ) ) {
107111
$newExpr->error( "invalid type for left operand: expected $leftType, got {$left->type}" );

src/Evaluator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static function evaluate( $number, array $rules ) {
3636
* @param array $rules The rules to compile
3737
* @return array An array of compile rules.
3838
*/
39-
public static function compile( array $rules ) {
39+
public static function compile( array $rules ) : array {
4040
// We can't use array_map() for this because it generates a warning if
4141
// there is an exception.
4242
foreach ( $rules as &$rule ) {
@@ -55,7 +55,7 @@ public static function compile( array $rules ) {
5555
* @param array $rules The associative array of plural rules in pluralform => rule format.
5656
* @return int The index of the plural form which passed the evaluation
5757
*/
58-
public static function evaluateCompiled( $number, array $rules ) {
58+
public static function evaluateCompiled( $number, array $rules ) : int {
5959
// Calculate the values of the operand symbols
6060
$number = strval( $number );
6161
if ( !preg_match( '/^ -? ( ([0-9]+) (?: \. ([0-9]+) )? )$/x', $number, $m ) ) {

src/Range.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function __construct( $start, $end = false ) {
4141
* otherwise, number simply has to be inside the range.
4242
* @return bool True if the number is inside the range; otherwise, false.
4343
*/
44-
public function isNumberIn( $number, $integerConstraint = true ) {
44+
public function isNumberIn( $number, $integerConstraint = true ) : bool {
4545
foreach ( $this->parts as $part ) {
4646
if ( is_array( $part ) ) {
4747
if ( ( !$integerConstraint || floor( $number ) === (float)$number )
@@ -66,7 +66,7 @@ public function isNumberIn( $number, $integerConstraint = true ) {
6666
* @param int $number The number to check
6767
* @return bool True if the number is inside the range; otherwise, false.
6868
*/
69-
public function isNumberWithin( $number ) {
69+
public function isNumberWithin( $number ) : bool {
7070
return $this->isNumberIn( $number, false );
7171
}
7272

0 commit comments

Comments
 (0)