Add phpcs and make pass

Change-Id: I8f985ca83223f1a2fdb4365294701f3b1d9b019b
This commit is contained in:
Umherirrender 2017-06-06 17:20:02 +02:00
parent 42ef20c152
commit 04e4c1b300
7 changed files with 130 additions and 103 deletions

View file

@ -48,7 +48,7 @@ define( 'EXPR_CEIL', 34 );
define( 'EXPR_POW', 35 ); define( 'EXPR_POW', 35 );
define( 'EXPR_PI', 36 ); define( 'EXPR_PI', 36 );
define( 'EXPR_FMOD', 37 ); define( 'EXPR_FMOD', 37 );
define( 'EXPR_SQRT' , 38 ); define( 'EXPR_SQRT', 38 );
class ExprError extends Exception { class ExprError extends Exception {
/** /**
@ -69,7 +69,7 @@ class ExprError extends Exception {
class ExprParser { class ExprParser {
public $maxStackSize = 100; public $maxStackSize = 100;
public $precedence = array( public $precedence = [
EXPR_NEGATIVE => 10, EXPR_NEGATIVE => 10,
EXPR_POSITIVE => 10, EXPR_POSITIVE => 10,
EXPR_EXPONENT => 10, EXPR_EXPONENT => 10,
@ -106,9 +106,9 @@ class ExprParser {
EXPR_PI => 0, EXPR_PI => 0,
EXPR_OPEN => -1, EXPR_OPEN => -1,
EXPR_CLOSE => -1, EXPR_CLOSE => -1,
); ];
public $names = array( public $names = [
EXPR_NEGATIVE => '-', EXPR_NEGATIVE => '-',
EXPR_POSITIVE => '+', EXPR_POSITIVE => '+',
EXPR_NOT => 'not', EXPR_NOT => 'not',
@ -143,9 +143,9 @@ class ExprParser {
EXPR_POW => '^', EXPR_POW => '^',
EXPR_PI => 'pi', EXPR_PI => 'pi',
EXPR_SQRT => 'sqrt', EXPR_SQRT => 'sqrt',
); ];
public $words = array( public $words = [
'mod' => EXPR_MOD, 'mod' => EXPR_MOD,
'fmod' => EXPR_FMOD, 'fmod' => EXPR_FMOD,
'and' => EXPR_AND, 'and' => EXPR_AND,
@ -168,7 +168,7 @@ class ExprParser {
'ceil' => EXPR_CEIL, 'ceil' => EXPR_CEIL,
'pi' => EXPR_PI, 'pi' => EXPR_PI,
'sqrt' => EXPR_SQRT, 'sqrt' => EXPR_SQRT,
); ];
/** /**
* Evaluate a mathematical expression * Evaluate a mathematical expression
@ -181,12 +181,12 @@ class ExprParser {
* @return string * @return string
*/ */
public function doExpression( $expr ) { public function doExpression( $expr ) {
$operands = array(); $operands = [];
$operators = array(); $operators = [];
# Unescape inequality operators # Unescape inequality operators
$expr = strtr( $expr, array( '&lt;' => '<', '&gt;' => '>', $expr = strtr( $expr, [ '&lt;' => '<', '&gt;' => '>',
'&minus;' => '-', '' => '-' ) ); '&minus;' => '-', '' => '-' ] );
$p = 0; $p = 0;
$end = strlen( $expr ); $end = strlen( $expr );
@ -239,7 +239,7 @@ class ExprParser {
throw new ExprError( 'unrecognised_word', $word ); throw new ExprError( 'unrecognised_word', $word );
} }
$op = $this->words[$word]; $op = $this->words[$word];
switch( $op ) { switch ( $op ) {
// constant // constant
case EXPR_EXPONENT: case EXPR_EXPONENT:
if ( $expecting !== 'expression' ) { if ( $expecting !== 'expression' ) {
@ -385,7 +385,9 @@ class ExprParser {
} }
// Finish off the operator array // Finish off the operator array
// @codingStandardsIgnoreStart
while ( $op = array_pop( $operators ) ) { while ( $op = array_pop( $operators ) ) {
// @codingStandardsIgnoreEnd
if ( $op == EXPR_OPEN ) { if ( $op == EXPR_OPEN ) {
throw new ExprError( 'unclosed_bracket' ); throw new ExprError( 'unclosed_bracket' );
} }
@ -657,9 +659,11 @@ class ExprParser {
} }
$right = array_pop( $stack ); $right = array_pop( $stack );
$left = 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] ); throw new ExprError( 'division_by_zero', $this->names[$op] );
} }
$stack[] = $result;
break; break;
case EXPR_SQRT: case EXPR_SQRT:
if ( count( $stack ) < 1 ) { if ( count( $stack ) < 1 ) {

View file

@ -37,13 +37,13 @@ class ParserFunctionsHooks {
// String Functions // String Functions
if ( $wgPFEnableStringFunctions ) { if ( $wgPFEnableStringFunctions ) {
$parser->setFunctionHook( 'len', 'ExtParserFunctions::runLen' ); $parser->setFunctionHook( 'len', 'ExtParserFunctions::runLen' );
$parser->setFunctionHook( 'pos', 'ExtParserFunctions::runPos' ); $parser->setFunctionHook( 'pos', 'ExtParserFunctions::runPos' );
$parser->setFunctionHook( 'rpos', 'ExtParserFunctions::runRPos' ); $parser->setFunctionHook( 'rpos', 'ExtParserFunctions::runRPos' );
$parser->setFunctionHook( 'sub', 'ExtParserFunctions::runSub' ); $parser->setFunctionHook( 'sub', 'ExtParserFunctions::runSub' );
$parser->setFunctionHook( 'count', 'ExtParserFunctions::runCount' ); $parser->setFunctionHook( 'count', 'ExtParserFunctions::runCount' );
$parser->setFunctionHook( 'replace', 'ExtParserFunctions::runReplace' ); $parser->setFunctionHook( 'replace', 'ExtParserFunctions::runReplace' );
$parser->setFunctionHook( 'explode', 'ExtParserFunctions::runExplode' ); $parser->setFunctionHook( 'explode', 'ExtParserFunctions::runExplode' );
$parser->setFunctionHook( 'urldecode', 'ExtParserFunctions::runUrlDecode' ); $parser->setFunctionHook( 'urldecode', 'ExtParserFunctions::runUrlDecode' );
} }

View file

@ -2,19 +2,19 @@
class Scribunto_LuaParserFunctionsLibrary extends Scribunto_LuaLibraryBase { class Scribunto_LuaParserFunctionsLibrary extends Scribunto_LuaLibraryBase {
public function register() { public function register() {
$lib = array( $lib = [
'expr' => array( $this, 'expr' ), 'expr' => [ $this, 'expr' ],
); ];
return $this->getEngine()->registerInterface( return $this->getEngine()->registerInterface(
__DIR__ . '/mw.ext.ParserFunctions.lua', $lib, array() __DIR__ . '/mw.ext.ParserFunctions.lua', $lib, []
); );
} }
public function expr( $expression = null ) { public function expr( $expression = null ) {
$this->checkType( 'mw.ext.ParserFunctions.expr', 1, $expression, 'string' ); $this->checkType( 'mw.ext.ParserFunctions.expr', 1, $expression, 'string' );
try { try {
return array( ExtParserFunctions::getExprParser()->doExpression( $expression ) ); return [ ExtParserFunctions::getExprParser()->doExpression( $expression ) ];
} catch ( ExprError $e ) { } catch ( ExprError $e ) {
throw new Scribunto_LuaError( $e->getMessage() ); throw new Scribunto_LuaError( $e->getMessage() );
} }

View file

@ -2,7 +2,7 @@
class ExtParserFunctions { class ExtParserFunctions {
public static $mExprParser; public static $mExprParser;
public static $mTimeCache = array(); public static $mTimeCache = [];
public static $mTimeChars = 0; public static $mTimeChars = 0;
public static $mMaxTimeChars = 6000; # ~10 seconds public static $mMaxTimeChars = 6000; # ~10 seconds
@ -22,7 +22,7 @@ class ExtParserFunctions {
*/ */
public static function registerClearHook() { public static function registerClearHook() {
static $done = false; static $done = false;
if( !$done ) { if ( !$done ) {
global $wgHooks; global $wgHooks;
$wgHooks['ParserClearState'][] = __CLASS__ . '::clearState'; $wgHooks['ParserClearState'][] = __CLASS__ . '::clearState';
$done = true; $done = true;
@ -248,7 +248,7 @@ class ExtParserFunctions {
$from = $parser->getTitle()->getPrefixedText(); $from = $parser->getTitle()->getPrefixedText();
} }
$to = rtrim( $to , ' /' ); $to = rtrim( $to, ' /' );
// if we have an empty path, or just one containing a dot // if we have an empty path, or just one containing a dot
if ( $to === '' || $to === '.' ) { if ( $to === '' || $to === '.' ) {
@ -256,9 +256,9 @@ class ExtParserFunctions {
} }
// if the path isn't relative // if the path isn't relative
if ( substr( $to , 0 , 1 ) !== '/' && if ( substr( $to, 0, 1 ) !== '/' &&
substr( $to , 0 , 2 ) !== './' && substr( $to, 0, 2 ) !== './' &&
substr( $to , 0 , 3 ) !== '../' && substr( $to, 0, 3 ) !== '../' &&
$to !== '..' ) $to !== '..' )
{ {
$from = ''; $from = '';
@ -273,9 +273,9 @@ class ExtParserFunctions {
$fullPath = preg_replace( '!/{2,}!', '/', $fullPath ); $fullPath = preg_replace( '!/{2,}!', '/', $fullPath );
// remove the enclosing slashes now // remove the enclosing slashes now
$fullPath = trim( $fullPath , '/' ); $fullPath = trim( $fullPath, '/' );
$exploded = explode ( '/' , $fullPath ); $exploded = explode( '/', $fullPath );
$newExploded = array(); $newExploded = [];
foreach ( $exploded as $current ) { foreach ( $exploded as $current ) {
if ( $current === '..' ) { // removing one level if ( $current === '..' ) { // removing one level
@ -294,7 +294,7 @@ class ExtParserFunctions {
} }
// we can now join it again // we can now join it again
return implode( '/' , $newExploded ); return implode( '/', $newExploded );
} }
/** /**
@ -350,7 +350,7 @@ class ExtParserFunctions {
$parser->mOutput->addLink( $title, 0 ); $parser->mOutput->addLink( $title, 0 );
return $else; return $else;
} }
if ( !$parser->incrementExpensiveFunctionCount() ) { if ( !$parser->incrementExpensiveFunctionCount() ) {
return $else; return $else;
} }
$id = $title->getArticleID(); $id = $title->getArticleID();
@ -410,7 +410,7 @@ class ExtParserFunctions {
if ( isset( self::$mTimeCache[$format][$cacheKey][$language][$local] ) ) { if ( isset( self::$mTimeCache[$format][$cacheKey][$language][$local] ) ) {
$cachedVal = self::$mTimeCache[$format][$cacheKey][$language][$local]; $cachedVal = self::$mTimeCache[$format][$cacheKey][$language][$local];
if ( $useTTL if ( $useTTL
&& $cachedVal[1] !== null && $frame && is_callable( array( $frame, 'setTTL' ) ) && $cachedVal[1] !== null && $frame && is_callable( [ $frame, 'setTTL' ] )
) { ) {
$frame->setTTL( $cachedVal[1] ); $frame->setTTL( $cachedVal[1] );
} }
@ -492,8 +492,8 @@ class ExtParserFunctions {
} }
} }
} }
self::$mTimeCache[$format][$cacheKey][$language][$local] = array( $result, $ttl ); self::$mTimeCache[$format][$cacheKey][$language][$local] = [ $result, $ttl ];
if ( $useTTL && $ttl !== null && $frame && is_callable( array( $frame, 'setTTL' ) ) ) { if ( $useTTL && $ttl !== null && $frame && is_callable( [ $frame, 'setTTL' ] ) ) {
$frame->setTTL( $ttl ); $frame->setTTL( $ttl );
} }
return $result; return $result;
@ -513,7 +513,6 @@ class ExtParserFunctions {
return self::timeCommon( $parser, null, $format, $date, $language, $local ); return self::timeCommon( $parser, null, $format, $date, $language, $local );
} }
/** /**
* @param $parser Parser * @param $parser Parser
* @param $frame PPFrame * @param $frame PPFrame
@ -613,7 +612,7 @@ class ExtParserFunctions {
* @param $inStr string * @param $inStr string
* @return int * @return int
*/ */
public static function runLen ( $parser, $inStr = '' ) { public static function runLen( $parser, $inStr = '' ) {
$inStr = $parser->killMarkers( (string)$inStr ); $inStr = $parser->killMarkers( (string)$inStr );
return mb_strlen( $inStr ); return mb_strlen( $inStr );
} }
@ -631,7 +630,7 @@ class ExtParserFunctions {
* @param $inOffset int * @param $inOffset int
* @return int|string * @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 ); $inStr = $parser->killMarkers( (string)$inStr );
$inNeedle = $parser->killMarkers( (string)$inNeedle ); $inNeedle = $parser->killMarkers( (string)$inNeedle );
@ -640,10 +639,14 @@ class ExtParserFunctions {
return self::tooLongError(); return self::tooLongError();
} }
if ( $inNeedle === '' ) { $inNeedle = ' '; } if ( $inNeedle === '' ) {
$inNeedle = ' ';
}
$pos = mb_strpos( $inStr, $inNeedle, (int)$inOffset ); $pos = mb_strpos( $inStr, $inNeedle, (int)$inOffset );
if ( $pos === false ) { $pos = ''; } if ( $pos === false ) {
$pos = '';
}
return $pos; return $pos;
} }
@ -660,7 +663,7 @@ class ExtParserFunctions {
* @param $inNeedle int|string * @param $inNeedle int|string
* @return int|string * @return int|string
*/ */
public static function runRPos ( $parser, $inStr = '', $inNeedle = '' ) { public static function runRPos( $parser, $inStr = '', $inNeedle = '' ) {
$inStr = $parser->killMarkers( (string)$inStr ); $inStr = $parser->killMarkers( (string)$inStr );
$inNeedle = $parser->killMarkers( (string)$inNeedle ); $inNeedle = $parser->killMarkers( (string)$inNeedle );
@ -669,10 +672,14 @@ class ExtParserFunctions {
return self::tooLongError(); return self::tooLongError();
} }
if ( $inNeedle === '' ) { $inNeedle = ' '; } if ( $inNeedle === '' ) {
$inNeedle = ' ';
}
$pos = mb_strrpos( $inStr, $inNeedle ); $pos = mb_strrpos( $inStr, $inNeedle );
if ( $pos === false ) { $pos = -1; } if ( $pos === false ) {
$pos = -1;
}
return $pos; return $pos;
} }
@ -695,7 +702,7 @@ class ExtParserFunctions {
* @param $inLength int * @param $inLength int
* @return string * @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 ); $inStr = $parser->killMarkers( (string)$inStr );
if ( !self::checkLength( $inStr ) ) { if ( !self::checkLength( $inStr ) ) {
@ -722,7 +729,7 @@ class ExtParserFunctions {
* @param $inSubStr string * @param $inSubStr string
* @return int|string * @return int|string
*/ */
public static function runCount ( $parser, $inStr = '', $inSubStr = '' ) { public static function runCount( $parser, $inStr = '', $inSubStr = '' ) {
$inStr = $parser->killMarkers( (string)$inStr ); $inStr = $parser->killMarkers( (string)$inStr );
$inSubStr = $parser->killMarkers( (string)$inSubStr ); $inSubStr = $parser->killMarkers( (string)$inSubStr );
@ -769,7 +776,9 @@ class ExtParserFunctions {
return self::tooLongError(); return self::tooLongError();
} }
if ( $inReplaceFrom === '' ) { $inReplaceFrom = ' '; } if ( $inReplaceFrom === '' ) {
$inReplaceFrom = ' ';
}
// Precompute limit to avoid generating enormous string: // Precompute limit to avoid generating enormous string:
$diff = mb_strlen( $inReplaceTo ) - mb_strlen( $inReplaceFrom ); $diff = mb_strlen( $inReplaceTo ) - mb_strlen( $inReplaceFrom );
@ -800,7 +809,6 @@ class ExtParserFunctions {
return $result; return $result;
} }
/** /**
* {{#explode:string | delimiter | position | limit}} * {{#explode:string | delimiter | position | limit}}
* *
@ -817,7 +825,7 @@ class ExtParserFunctions {
* @param $inLim int|null * @param $inLim int|null
* @return string * @return string
*/ */
public static function runExplode ( public static function runExplode(
$parser, $inStr = '', $inDiv = '', $inPos = 0, $inLim = null $parser, $inStr = '', $inDiv = '', $inPos = 0, $inLim = null
) { ) {
$inStr = $parser->killMarkers( (string)$inStr ); $inStr = $parser->killMarkers( (string)$inStr );

View file

@ -1,11 +1,14 @@
{ {
"require-dev": { "require-dev": {
"jakub-onderka/php-parallel-lint": "0.9.2", "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": { "scripts": {
"fix": "phpcbf",
"test": [ "test": [
"parallel-lint . --exclude node_modules --exclude vendor" "parallel-lint . --exclude node_modules --exclude vendor",
"phpcs -p -s"
] ]
} }
} }

12
phpcs.xml Normal file
View file

@ -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>

View file

@ -22,55 +22,55 @@ class ExpressionTest extends MediaWikiTestCase {
} }
function provideExpressions() { function provideExpressions() {
return array( return [
array( '1 or 0', '1' ), [ '1 or 0', '1' ],
array( 'not (1 and 0)', '1' ), [ 'not (1 and 0)', '1' ],
array( 'not 0', '1' ), [ 'not 0', '1' ],
array( '4 < 5', '1' ), [ '4 < 5', '1' ],
array( '-5 < 2', '1' ), [ '-5 < 2', '1' ],
array( '-2 <= -2', '1' ), [ '-2 <= -2', '1' ],
array( '4 > 3', '1' ), [ '4 > 3', '1' ],
array( '4 > -3', '1' ), [ '4 > -3', '1' ],
array( '5 >= 2', '1' ), [ '5 >= 2', '1' ],
array( '2 >= 2', '1' ), [ '2 >= 2', '1' ],
array( '1 != 2', '1' ), [ '1 != 2', '1' ],
array( '-4 * -4 = 4 * 4', '1' ), [ '-4 * -4 = 4 * 4', '1' ],
array( 'not (1 != 1)', '1' ), [ 'not (1 != 1)', '1' ],
array( '1 + 1', '2' ), [ '1 + 1', '2' ],
array( '-1 + 1', '0' ), [ '-1 + 1', '0' ],
array( '+1 + 1', '2' ), [ '+1 + 1', '2' ],
array( '4 * 4', '16' ), [ '4 * 4', '16' ],
array( '(1/3) * 3', '1' ), [ '(1/3) * 3', '1' ],
array( '3 / 1.5', '2' ), [ '3 / 1.5', '2' ],
array( '3 / 0.2', '15' ), [ '3 / 0.2', '15' ],
array( '3 / ( 2.0 * 0.1 )', '15' ), [ '3 / ( 2.0 * 0.1 )', '15' ],
array( '3 / ( 2.0 / 10 )', '15' ), [ '3 / ( 2.0 / 10 )', '15' ],
array( '3 / (- 0.2 )', '-15' ), [ '3 / (- 0.2 )', '-15' ],
array( '3 / abs( 0.2 )', '15' ), [ '3 / abs( 0.2 )', '15' ],
array( '3 mod 2', '1' ), [ '3 mod 2', '1' ],
array( '1e4', '10000' ), [ '1e4', '10000' ],
array( '1e-2', '0.01' ), [ '1e-2', '0.01' ],
array( '4.0 round 0', '4' ), [ '4.0 round 0', '4' ],
array( 'ceil 4', '4' ), [ 'ceil 4', '4' ],
array( 'floor 4', '4' ), [ 'floor 4', '4' ],
array( '4.5 round 0', '5' ), [ '4.5 round 0', '5' ],
array( '4.2 round 0', '4' ), [ '4.2 round 0', '4' ],
array( '-4.2 round 0', '-4' ), [ '-4.2 round 0', '-4' ],
array( '-4.5 round 0', '-5' ), [ '-4.5 round 0', '-5' ],
array( '-2.0 round 0', '-2' ), [ '-2.0 round 0', '-2' ],
array( 'ceil -3', '-3' ), [ 'ceil -3', '-3' ],
array( 'floor -6.0', '-6' ), [ 'floor -6.0', '-6' ],
array( 'ceil 4.2', '5' ), [ 'ceil 4.2', '5' ],
array( 'ceil -4.5', '-4' ), [ 'ceil -4.5', '-4' ],
array( 'floor -4.5', '-5' ), [ 'floor -4.5', '-5' ],
array( 'abs(-2)', '2' ), [ 'abs(-2)', '2' ],
array( 'ln(exp(1))', '1' ), [ 'ln(exp(1))', '1' ],
array( 'trunc(4.5)', '4' ), [ 'trunc(4.5)', '4' ],
array( 'trunc(-4.5)', '-4' ), [ 'trunc(-4.5)', '-4' ],
array( '123 fmod (2^64-1)', '123' ), [ '123 fmod (2^64-1)', '123' ],
array( '5.7 mod 1.3', '0' ), [ '5.7 mod 1.3', '0' ],
array( '5.7 fmod 1.3', '0.5' ), [ '5.7 fmod 1.3', '0.5' ],
); ];
} }
} }