jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/357399 )
Change subject: Add phpcs and make pass ...................................................................... Add phpcs and make pass Change-Id: I8f985ca83223f1a2fdb4365294701f3b1d9b019b --- M Expr.php M ParserFunctions.hooks.php M ParserFunctions.library.php M ParserFunctions_body.php M composer.json A phpcs.xml M tests/phpunit/ExpressionTest.php 7 files changed, 130 insertions(+), 103 deletions(-) Approvals: jenkins-bot: Verified Jforrester: Looks good to me, approved diff --git a/Expr.php b/Expr.php index d484e09..2ef3ece 100644 --- a/Expr.php +++ b/Expr.php @@ -48,7 +48,7 @@ define( 'EXPR_POW', 35 ); define( 'EXPR_PI', 36 ); define( 'EXPR_FMOD', 37 ); -define( 'EXPR_SQRT' , 38 ); +define( 'EXPR_SQRT', 38 ); class ExprError extends Exception { /** @@ -69,7 +69,7 @@ class ExprParser { public $maxStackSize = 100; - public $precedence = array( + public $precedence = [ EXPR_NEGATIVE => 10, EXPR_POSITIVE => 10, EXPR_EXPONENT => 10, @@ -106,9 +106,9 @@ EXPR_PI => 0, EXPR_OPEN => -1, EXPR_CLOSE => -1, - ); + ]; - public $names = array( + public $names = [ EXPR_NEGATIVE => '-', EXPR_POSITIVE => '+', EXPR_NOT => 'not', @@ -143,9 +143,9 @@ EXPR_POW => '^', EXPR_PI => 'pi', EXPR_SQRT => 'sqrt', - ); + ]; - public $words = array( + public $words = [ 'mod' => EXPR_MOD, 'fmod' => EXPR_FMOD, 'and' => EXPR_AND, @@ -168,7 +168,7 @@ 'ceil' => EXPR_CEIL, 'pi' => EXPR_PI, 'sqrt' => EXPR_SQRT, - ); + ]; /** * Evaluate a mathematical expression @@ -181,12 +181,12 @@ * @return string */ public function doExpression( $expr ) { - $operands = array(); - $operators = array(); + $operands = []; + $operators = []; # Unescape inequality operators - $expr = strtr( $expr, array( '<' => '<', '>' => '>', - '−' => '-', '−' => '-' ) ); + $expr = strtr( $expr, [ '<' => '<', '>' => '>', + '−' => '-', '−' => '-' ] ); $p = 0; $end = strlen( $expr ); @@ -239,7 +239,7 @@ throw new ExprError( 'unrecognised_word', $word ); } $op = $this->words[$word]; - switch( $op ) { + switch ( $op ) { // constant case EXPR_EXPONENT: if ( $expecting !== 'expression' ) { @@ -385,7 +385,9 @@ } // Finish off the operator array + // @codingStandardsIgnoreStart while ( $op = array_pop( $operators ) ) { + // @codingStandardsIgnoreEnd if ( $op == EXPR_OPEN ) { throw new ExprError( 'unclosed_bracket' ); } @@ -657,9 +659,11 @@ } $right = array_pop( $stack ); $left = array_pop( $stack ); - if ( false === ( $stack[] = pow( $left, $right ) ) ) { + $result = pow( $left, $right ); + if ( $result === false ) { throw new ExprError( 'division_by_zero', $this->names[$op] ); } + $stack[] = $result; break; case EXPR_SQRT: if ( count( $stack ) < 1 ) { diff --git a/ParserFunctions.hooks.php b/ParserFunctions.hooks.php index b204333..646f8e7 100644 --- a/ParserFunctions.hooks.php +++ b/ParserFunctions.hooks.php @@ -37,13 +37,13 @@ // String Functions if ( $wgPFEnableStringFunctions ) { - $parser->setFunctionHook( 'len', 'ExtParserFunctions::runLen' ); - $parser->setFunctionHook( 'pos', 'ExtParserFunctions::runPos' ); - $parser->setFunctionHook( 'rpos', 'ExtParserFunctions::runRPos' ); - $parser->setFunctionHook( 'sub', 'ExtParserFunctions::runSub' ); - $parser->setFunctionHook( 'count', 'ExtParserFunctions::runCount' ); - $parser->setFunctionHook( 'replace', 'ExtParserFunctions::runReplace' ); - $parser->setFunctionHook( 'explode', 'ExtParserFunctions::runExplode' ); + $parser->setFunctionHook( 'len', 'ExtParserFunctions::runLen' ); + $parser->setFunctionHook( 'pos', 'ExtParserFunctions::runPos' ); + $parser->setFunctionHook( 'rpos', 'ExtParserFunctions::runRPos' ); + $parser->setFunctionHook( 'sub', 'ExtParserFunctions::runSub' ); + $parser->setFunctionHook( 'count', 'ExtParserFunctions::runCount' ); + $parser->setFunctionHook( 'replace', 'ExtParserFunctions::runReplace' ); + $parser->setFunctionHook( 'explode', 'ExtParserFunctions::runExplode' ); $parser->setFunctionHook( 'urldecode', 'ExtParserFunctions::runUrlDecode' ); } diff --git a/ParserFunctions.library.php b/ParserFunctions.library.php index d817256..7dc7c59 100644 --- a/ParserFunctions.library.php +++ b/ParserFunctions.library.php @@ -2,19 +2,19 @@ class Scribunto_LuaParserFunctionsLibrary extends Scribunto_LuaLibraryBase { public function register() { - $lib = array( - 'expr' => array( $this, 'expr' ), - ); + $lib = [ + 'expr' => [ $this, 'expr' ], + ]; return $this->getEngine()->registerInterface( - __DIR__ . '/mw.ext.ParserFunctions.lua', $lib, array() + __DIR__ . '/mw.ext.ParserFunctions.lua', $lib, [] ); } public function expr( $expression = null ) { $this->checkType( 'mw.ext.ParserFunctions.expr', 1, $expression, 'string' ); try { - return array( ExtParserFunctions::getExprParser()->doExpression( $expression ) ); + return [ ExtParserFunctions::getExprParser()->doExpression( $expression ) ]; } catch ( ExprError $e ) { throw new Scribunto_LuaError( $e->getMessage() ); } diff --git a/ParserFunctions_body.php b/ParserFunctions_body.php index d69dc62..b387570 100644 --- a/ParserFunctions_body.php +++ b/ParserFunctions_body.php @@ -2,7 +2,7 @@ class ExtParserFunctions { public static $mExprParser; - public static $mTimeCache = array(); + public static $mTimeCache = []; public static $mTimeChars = 0; public static $mMaxTimeChars = 6000; # ~10 seconds @@ -22,7 +22,7 @@ */ public static function registerClearHook() { static $done = false; - if( !$done ) { + if ( !$done ) { global $wgHooks; $wgHooks['ParserClearState'][] = __CLASS__ . '::clearState'; $done = true; @@ -248,7 +248,7 @@ $from = $parser->getTitle()->getPrefixedText(); } - $to = rtrim( $to , ' /' ); + $to = rtrim( $to, ' /' ); // if we have an empty path, or just one containing a dot if ( $to === '' || $to === '.' ) { @@ -256,9 +256,9 @@ } // if the path isn't relative - if ( substr( $to , 0 , 1 ) !== '/' && - substr( $to , 0 , 2 ) !== './' && - substr( $to , 0 , 3 ) !== '../' && + if ( substr( $to, 0, 1 ) !== '/' && + substr( $to, 0, 2 ) !== './' && + substr( $to, 0, 3 ) !== '../' && $to !== '..' ) { $from = ''; @@ -273,9 +273,9 @@ $fullPath = preg_replace( '!/{2,}!', '/', $fullPath ); // remove the enclosing slashes now - $fullPath = trim( $fullPath , '/' ); - $exploded = explode ( '/' , $fullPath ); - $newExploded = array(); + $fullPath = trim( $fullPath, '/' ); + $exploded = explode( '/', $fullPath ); + $newExploded = []; foreach ( $exploded as $current ) { if ( $current === '..' ) { // removing one level @@ -294,7 +294,7 @@ } // we can now join it again - return implode( '/' , $newExploded ); + return implode( '/', $newExploded ); } /** @@ -350,7 +350,7 @@ $parser->mOutput->addLink( $title, 0 ); return $else; } - if ( !$parser->incrementExpensiveFunctionCount() ) { + if ( !$parser->incrementExpensiveFunctionCount() ) { return $else; } $id = $title->getArticleID(); @@ -410,7 +410,7 @@ if ( isset( self::$mTimeCache[$format][$cacheKey][$language][$local] ) ) { $cachedVal = self::$mTimeCache[$format][$cacheKey][$language][$local]; if ( $useTTL - && $cachedVal[1] !== null && $frame && is_callable( array( $frame, 'setTTL' ) ) + && $cachedVal[1] !== null && $frame && is_callable( [ $frame, 'setTTL' ] ) ) { $frame->setTTL( $cachedVal[1] ); } @@ -492,8 +492,8 @@ } } } - self::$mTimeCache[$format][$cacheKey][$language][$local] = array( $result, $ttl ); - if ( $useTTL && $ttl !== null && $frame && is_callable( array( $frame, 'setTTL' ) ) ) { + self::$mTimeCache[$format][$cacheKey][$language][$local] = [ $result, $ttl ]; + if ( $useTTL && $ttl !== null && $frame && is_callable( [ $frame, 'setTTL' ] ) ) { $frame->setTTL( $ttl ); } return $result; @@ -512,7 +512,6 @@ ) { return self::timeCommon( $parser, null, $format, $date, $language, $local ); } - /** * @param $parser Parser @@ -613,7 +612,7 @@ * @param $inStr string * @return int */ - public static function runLen ( $parser, $inStr = '' ) { + public static function runLen( $parser, $inStr = '' ) { $inStr = $parser->killMarkers( (string)$inStr ); return mb_strlen( $inStr ); } @@ -631,7 +630,7 @@ * @param $inOffset int * @return int|string */ - public static function runPos ( $parser, $inStr = '', $inNeedle = '', $inOffset = 0 ) { + public static function runPos( $parser, $inStr = '', $inNeedle = '', $inOffset = 0 ) { $inStr = $parser->killMarkers( (string)$inStr ); $inNeedle = $parser->killMarkers( (string)$inNeedle ); @@ -640,10 +639,14 @@ return self::tooLongError(); } - if ( $inNeedle === '' ) { $inNeedle = ' '; } + if ( $inNeedle === '' ) { + $inNeedle = ' '; + } $pos = mb_strpos( $inStr, $inNeedle, (int)$inOffset ); - if ( $pos === false ) { $pos = ''; } + if ( $pos === false ) { + $pos = ''; + } return $pos; } @@ -660,7 +663,7 @@ * @param $inNeedle int|string * @return int|string */ - public static function runRPos ( $parser, $inStr = '', $inNeedle = '' ) { + public static function runRPos( $parser, $inStr = '', $inNeedle = '' ) { $inStr = $parser->killMarkers( (string)$inStr ); $inNeedle = $parser->killMarkers( (string)$inNeedle ); @@ -669,10 +672,14 @@ return self::tooLongError(); } - if ( $inNeedle === '' ) { $inNeedle = ' '; } + if ( $inNeedle === '' ) { + $inNeedle = ' '; + } $pos = mb_strrpos( $inStr, $inNeedle ); - if ( $pos === false ) { $pos = -1; } + if ( $pos === false ) { + $pos = -1; + } return $pos; } @@ -695,7 +702,7 @@ * @param $inLength int * @return string */ - public static function runSub ( $parser, $inStr = '', $inStart = 0, $inLength = 0 ) { + public static function runSub( $parser, $inStr = '', $inStart = 0, $inLength = 0 ) { $inStr = $parser->killMarkers( (string)$inStr ); if ( !self::checkLength( $inStr ) ) { @@ -722,7 +729,7 @@ * @param $inSubStr string * @return int|string */ - public static function runCount ( $parser, $inStr = '', $inSubStr = '' ) { + public static function runCount( $parser, $inStr = '', $inSubStr = '' ) { $inStr = $parser->killMarkers( (string)$inStr ); $inSubStr = $parser->killMarkers( (string)$inSubStr ); @@ -769,7 +776,9 @@ return self::tooLongError(); } - if ( $inReplaceFrom === '' ) { $inReplaceFrom = ' '; } + if ( $inReplaceFrom === '' ) { + $inReplaceFrom = ' '; + } // Precompute limit to avoid generating enormous string: $diff = mb_strlen( $inReplaceTo ) - mb_strlen( $inReplaceFrom ); @@ -800,7 +809,6 @@ return $result; } - /** * {{#explode:string | delimiter | position | limit}} * @@ -817,7 +825,7 @@ * @param $inLim int|null * @return string */ - public static function runExplode ( + public static function runExplode( $parser, $inStr = '', $inDiv = '', $inPos = 0, $inLim = null ) { $inStr = $parser->killMarkers( (string)$inStr ); diff --git a/composer.json b/composer.json index a3aecbe..e4e502f 100644 --- a/composer.json +++ b/composer.json @@ -1,11 +1,14 @@ { "require-dev": { "jakub-onderka/php-parallel-lint": "0.9.2", - "jakub-onderka/php-console-highlighter": "0.3.2" + "jakub-onderka/php-console-highlighter": "0.3.2", + "mediawiki/mediawiki-codesniffer": "0.7.2" }, "scripts": { + "fix": "phpcbf", "test": [ - "parallel-lint . --exclude node_modules --exclude vendor" + "parallel-lint . --exclude node_modules --exclude vendor", + "phpcs -p -s" ] } } diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..414e175 --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,12 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ruleset> + <rule ref="vendor/mediawiki/mediawiki-codesniffer/MediaWiki"> + <exclude name="Squiz.Classes.ValidClassName.NotCamelCaps"/> + <exclude name="MediaWiki.ControlStructures.IfElseStructure"/> + </rule> + <file>.</file> + <arg name="extensions" value="php,php5,inc"/> + <arg name="encoding" value="UTF-8"/> + <exclude-pattern>vendor</exclude-pattern> + <exclude-pattern>node_modules</exclude-pattern> +</ruleset> diff --git a/tests/phpunit/ExpressionTest.php b/tests/phpunit/ExpressionTest.php index 169a9cb..a7d290d 100644 --- a/tests/phpunit/ExpressionTest.php +++ b/tests/phpunit/ExpressionTest.php @@ -22,55 +22,55 @@ } function provideExpressions() { - return array( - array( '1 or 0', '1' ), - array( 'not (1 and 0)', '1' ), - array( 'not 0', '1' ), - array( '4 < 5', '1' ), - array( '-5 < 2', '1' ), - array( '-2 <= -2', '1' ), - array( '4 > 3', '1' ), - array( '4 > -3', '1' ), - array( '5 >= 2', '1' ), - array( '2 >= 2', '1' ), - array( '1 != 2', '1' ), - array( '-4 * -4 = 4 * 4', '1' ), - array( 'not (1 != 1)', '1' ), - array( '1 + 1', '2' ), - array( '-1 + 1', '0' ), - array( '+1 + 1', '2' ), - array( '4 * 4', '16' ), - array( '(1/3) * 3', '1' ), - array( '3 / 1.5', '2' ), - array( '3 / 0.2', '15' ), - array( '3 / ( 2.0 * 0.1 )', '15' ), - array( '3 / ( 2.0 / 10 )', '15' ), - array( '3 / (- 0.2 )', '-15' ), - array( '3 / abs( 0.2 )', '15' ), - array( '3 mod 2', '1' ), - array( '1e4', '10000' ), - array( '1e-2', '0.01' ), - array( '4.0 round 0', '4' ), - array( 'ceil 4', '4' ), - array( 'floor 4', '4' ), - array( '4.5 round 0', '5' ), - array( '4.2 round 0', '4' ), - array( '-4.2 round 0', '-4' ), - array( '-4.5 round 0', '-5' ), - array( '-2.0 round 0', '-2' ), - array( 'ceil -3', '-3' ), - array( 'floor -6.0', '-6' ), - array( 'ceil 4.2', '5' ), - array( 'ceil -4.5', '-4' ), - array( 'floor -4.5', '-5' ), - array( 'abs(-2)', '2' ), - array( 'ln(exp(1))', '1' ), - array( 'trunc(4.5)', '4' ), - array( 'trunc(-4.5)', '-4' ), - array( '123 fmod (2^64-1)', '123' ), - array( '5.7 mod 1.3', '0' ), - array( '5.7 fmod 1.3', '0.5' ), - ); + return [ + [ '1 or 0', '1' ], + [ 'not (1 and 0)', '1' ], + [ 'not 0', '1' ], + [ '4 < 5', '1' ], + [ '-5 < 2', '1' ], + [ '-2 <= -2', '1' ], + [ '4 > 3', '1' ], + [ '4 > -3', '1' ], + [ '5 >= 2', '1' ], + [ '2 >= 2', '1' ], + [ '1 != 2', '1' ], + [ '-4 * -4 = 4 * 4', '1' ], + [ 'not (1 != 1)', '1' ], + [ '1 + 1', '2' ], + [ '-1 + 1', '0' ], + [ '+1 + 1', '2' ], + [ '4 * 4', '16' ], + [ '(1/3) * 3', '1' ], + [ '3 / 1.5', '2' ], + [ '3 / 0.2', '15' ], + [ '3 / ( 2.0 * 0.1 )', '15' ], + [ '3 / ( 2.0 / 10 )', '15' ], + [ '3 / (- 0.2 )', '-15' ], + [ '3 / abs( 0.2 )', '15' ], + [ '3 mod 2', '1' ], + [ '1e4', '10000' ], + [ '1e-2', '0.01' ], + [ '4.0 round 0', '4' ], + [ 'ceil 4', '4' ], + [ 'floor 4', '4' ], + [ '4.5 round 0', '5' ], + [ '4.2 round 0', '4' ], + [ '-4.2 round 0', '-4' ], + [ '-4.5 round 0', '-5' ], + [ '-2.0 round 0', '-2' ], + [ 'ceil -3', '-3' ], + [ 'floor -6.0', '-6' ], + [ 'ceil 4.2', '5' ], + [ 'ceil -4.5', '-4' ], + [ 'floor -4.5', '-5' ], + [ 'abs(-2)', '2' ], + [ 'ln(exp(1))', '1' ], + [ 'trunc(4.5)', '4' ], + [ 'trunc(-4.5)', '-4' ], + [ '123 fmod (2^64-1)', '123' ], + [ '5.7 mod 1.3', '0' ], + [ '5.7 fmod 1.3', '0.5' ], + ]; } } -- To view, visit https://gerrit.wikimedia.org/r/357399 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I8f985ca83223f1a2fdb4365294701f3b1d9b019b Gerrit-PatchSet: 2 Gerrit-Project: mediawiki/extensions/ParserFunctions Gerrit-Branch: master Gerrit-Owner: Umherirrender <umherirrender_de...@web.de> Gerrit-Reviewer: Jackmcbarn <jackmcb...@gmail.com> Gerrit-Reviewer: Jforrester <jforres...@wikimedia.org> Gerrit-Reviewer: Legoktm <lego...@member.fsf.org> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits