From a3dbd6c257ee8b2ca1af9656d8305049c4a3ebee Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Tue, 23 Apr 2019 21:27:03 +0200 Subject: [PATCH] Updated coding standard --- composer.json | 5 +- phpcs.xml | 56 +++++++++++++------ src/Ast/ConstExpr/ConstExprArrayItemNode.php | 6 +- src/Ast/ConstExpr/ConstFetchNode.php | 4 +- src/Ast/PhpDoc/MethodTagValueNode.php | 2 +- .../PhpDoc/MethodTagValueParameterNode.php | 4 +- src/Ast/PhpDoc/PhpDocNode.php | 20 +++---- src/Lexer/Lexer.php | 6 +- src/Parser/ConstExprParser.php | 4 +- src/Parser/PhpDocParser.php | 10 ++-- src/Parser/TokenIterator.php | 30 ++++++---- tests/PHPStan/Parser/ConstExprParserTest.php | 4 +- tests/PHPStan/Parser/FuzzyTest.php | 6 +- tests/PHPStan/Parser/PhpDocParserTest.php | 46 +++++++-------- tests/PHPStan/Parser/TypeParserTest.php | 4 +- 15 files changed, 119 insertions(+), 88 deletions(-) diff --git a/composer.json b/composer.json index 9fbf15a..332c8be 100644 --- a/composer.json +++ b/composer.json @@ -6,12 +6,13 @@ "php": "~7.1" }, "require-dev": { - "consistence/coding-standard": "^2.0.0", + "consistence/coding-standard": "^3.5", "jakub-onderka/php-parallel-lint": "^0.9.2", "phing/phing": "^2.16.0", "phpstan/phpstan": "^0.10", "phpunit/phpunit": "^6.3", - "slevomat/coding-standard": "^3.3.0", + "slevomat/coding-standard": "^4.7.2", + "squizlabs/php_codesniffer": "^3.3.2", "symfony/process": "^3.4 || ^4.0" }, "autoload": { diff --git a/phpcs.xml b/phpcs.xml index ee8b7ff..0e499d2 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -1,21 +1,14 @@ - - - - - - + - + - - - - - - + + + + @@ -31,11 +24,40 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + tests/*/data + tests/*/traits + tests/notAutoloaded + src/Reflection/SignatureMap/functionMap.php diff --git a/src/Ast/ConstExpr/ConstExprArrayItemNode.php b/src/Ast/ConstExpr/ConstExprArrayItemNode.php index 61bdd75..9456e95 100644 --- a/src/Ast/ConstExpr/ConstExprArrayItemNode.php +++ b/src/Ast/ConstExpr/ConstExprArrayItemNode.php @@ -5,7 +5,7 @@ namespace PHPStan\PhpDocParser\Ast\ConstExpr; class ConstExprArrayItemNode implements ConstExprNode { - /** @var null|ConstExprNode */ + /** @var ConstExprNode|null */ public $key; /** @var ConstExprNode */ @@ -23,9 +23,9 @@ class ConstExprArrayItemNode implements ConstExprNode if ($this->key !== null) { return "{$this->key} => {$this->value}"; - } else { - return "{$this->value}"; } + + return "{$this->value}"; } } diff --git a/src/Ast/ConstExpr/ConstFetchNode.php b/src/Ast/ConstExpr/ConstFetchNode.php index d73a47c..a432741 100644 --- a/src/Ast/ConstExpr/ConstFetchNode.php +++ b/src/Ast/ConstExpr/ConstFetchNode.php @@ -23,9 +23,9 @@ class ConstFetchNode implements ConstExprNode if ($this->className === '') { return $this->name; - } else { - return "{$this->className}::{$this->name}"; } + + return "{$this->className}::{$this->name}"; } } diff --git a/src/Ast/PhpDoc/MethodTagValueNode.php b/src/Ast/PhpDoc/MethodTagValueNode.php index ec86e50..d7b4590 100644 --- a/src/Ast/PhpDoc/MethodTagValueNode.php +++ b/src/Ast/PhpDoc/MethodTagValueNode.php @@ -10,7 +10,7 @@ class MethodTagValueNode implements PhpDocTagValueNode /** @var bool */ public $isStatic; - /** @var null|TypeNode */ + /** @var TypeNode|null */ public $returnType; /** @var string */ diff --git a/src/Ast/PhpDoc/MethodTagValueParameterNode.php b/src/Ast/PhpDoc/MethodTagValueParameterNode.php index 6bcadd7..87c7679 100644 --- a/src/Ast/PhpDoc/MethodTagValueParameterNode.php +++ b/src/Ast/PhpDoc/MethodTagValueParameterNode.php @@ -9,7 +9,7 @@ use PHPStan\PhpDocParser\Ast\Type\TypeNode; class MethodTagValueParameterNode implements Node { - /** @var null|TypeNode */ + /** @var TypeNode|null */ public $type; /** @var bool */ @@ -21,7 +21,7 @@ class MethodTagValueParameterNode implements Node /** @var string */ public $parameterName; - /** @var null|ConstExprNode */ + /** @var ConstExprNode|null */ public $defaultValue; public function __construct(?TypeNode $type, bool $isReference, bool $isVariadic, string $parameterName, ?ConstExprNode $defaultValue) diff --git a/src/Ast/PhpDoc/PhpDocNode.php b/src/Ast/PhpDoc/PhpDocNode.php index e4cdf57..4618843 100644 --- a/src/Ast/PhpDoc/PhpDocNode.php +++ b/src/Ast/PhpDoc/PhpDocNode.php @@ -24,7 +24,7 @@ class PhpDocNode implements Node */ public function getTags(): array { - return array_filter($this->children, function (PhpDocChildNode $child): bool { + return array_filter($this->children, static function (PhpDocChildNode $child): bool { return $child instanceof PhpDocTagNode; }); } @@ -36,7 +36,7 @@ class PhpDocNode implements Node */ public function getTagsByName(string $tagName): array { - return array_filter($this->getTags(), function (PhpDocTagNode $tag) use ($tagName): bool { + return array_filter($this->getTags(), static function (PhpDocTagNode $tag) use ($tagName): bool { return $tag->name === $tagName; }); } @@ -48,7 +48,7 @@ class PhpDocNode implements Node public function getVarTagValues(): array { return array_column( - array_filter($this->getTagsByName('@var'), function (PhpDocTagNode $tag): bool { + array_filter($this->getTagsByName('@var'), static function (PhpDocTagNode $tag): bool { return $tag->value instanceof VarTagValueNode; }), 'value' @@ -62,7 +62,7 @@ class PhpDocNode implements Node public function getParamTagValues(): array { return array_column( - array_filter($this->getTagsByName('@param'), function (PhpDocTagNode $tag): bool { + array_filter($this->getTagsByName('@param'), static function (PhpDocTagNode $tag): bool { return $tag->value instanceof ParamTagValueNode; }), 'value' @@ -76,7 +76,7 @@ class PhpDocNode implements Node public function getReturnTagValues(): array { return array_column( - array_filter($this->getTagsByName('@return'), function (PhpDocTagNode $tag): bool { + array_filter($this->getTagsByName('@return'), static function (PhpDocTagNode $tag): bool { return $tag->value instanceof ReturnTagValueNode; }), 'value' @@ -90,7 +90,7 @@ class PhpDocNode implements Node public function getThrowsTagValues(): array { return array_column( - array_filter($this->getTagsByName('@throws'), function (PhpDocTagNode $tag): bool { + array_filter($this->getTagsByName('@throws'), static function (PhpDocTagNode $tag): bool { return $tag->value instanceof ThrowsTagValueNode; }), 'value' @@ -104,7 +104,7 @@ class PhpDocNode implements Node public function getPropertyTagValues(): array { return array_column( - array_filter($this->getTagsByName('@property'), function (PhpDocTagNode $tag): bool { + array_filter($this->getTagsByName('@property'), static function (PhpDocTagNode $tag): bool { return $tag->value instanceof PropertyTagValueNode; }), 'value' @@ -118,7 +118,7 @@ class PhpDocNode implements Node public function getPropertyReadTagValues(): array { return array_column( - array_filter($this->getTagsByName('@property-read'), function (PhpDocTagNode $tag): bool { + array_filter($this->getTagsByName('@property-read'), static function (PhpDocTagNode $tag): bool { return $tag->value instanceof PropertyTagValueNode; }), 'value' @@ -132,7 +132,7 @@ class PhpDocNode implements Node public function getPropertyWriteTagValues(): array { return array_column( - array_filter($this->getTagsByName('@property-write'), function (PhpDocTagNode $tag): bool { + array_filter($this->getTagsByName('@property-write'), static function (PhpDocTagNode $tag): bool { return $tag->value instanceof PropertyTagValueNode; }), 'value' @@ -146,7 +146,7 @@ class PhpDocNode implements Node public function getMethodTagValues(): array { return array_column( - array_filter($this->getTagsByName('@method'), function (PhpDocTagNode $tag): bool { + array_filter($this->getTagsByName('@method'), static function (PhpDocTagNode $tag): bool { return $tag->value instanceof MethodTagValueNode; }), 'value' diff --git a/src/Lexer/Lexer.php b/src/Lexer/Lexer.php index f31dbb0..841b0a1 100644 --- a/src/Lexer/Lexer.php +++ b/src/Lexer/Lexer.php @@ -75,10 +75,10 @@ class Lexer public const VALUE_OFFSET = 0; public const TYPE_OFFSET = 1; - /** @var null|string */ + /** @var string|null */ private $regexp; - /** @var null|int[] */ + /** @var int[]|null */ private $types; public function tokenize(string $s): array @@ -108,7 +108,7 @@ class Lexer } - private function initialize() + private function initialize(): void { $patterns = [ // '&' followed by TOKEN_VARIADIC, TOKEN_VARIABLE, TOKEN_EQUAL, TOKEN_EQUAL or TOKEN_CLOSE_PARENTHESES diff --git a/src/Parser/ConstExprParser.php b/src/Parser/ConstExprParser.php index a9dcdbb..3cb92ba 100644 --- a/src/Parser/ConstExprParser.php +++ b/src/Parser/ConstExprParser.php @@ -51,10 +51,10 @@ class ConstExprParser $tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER); return new Ast\ConstExpr\ConstFetchNode($identifier, $classConstantName); - } else { - return new Ast\ConstExpr\ConstFetchNode('', $identifier); } + return new Ast\ConstExpr\ConstFetchNode('', $identifier); + } elseif ($tokens->tryConsumeTokenType(Lexer::TOKEN_OPEN_SQUARE_BRACKET)) { return $this->parseArray($tokens, Lexer::TOKEN_CLOSE_SQUARE_BRACKET); } diff --git a/src/Parser/PhpDocParser.php b/src/Parser/PhpDocParser.php index e5370a5..17db772 100644 --- a/src/Parser/PhpDocParser.php +++ b/src/Parser/PhpDocParser.php @@ -52,9 +52,9 @@ class PhpDocParser if ($tokens->isCurrentTokenType(Lexer::TOKEN_PHPDOC_TAG)) { return $this->parseTag($tokens); - } else { - return $this->parseText($tokens); } + + return $this->parseText($tokens); } @@ -261,9 +261,11 @@ class PhpDocParser { if ($limitStartToken) { foreach (self::DISALLOWED_DESCRIPTION_START_TOKENS as $disallowedStartToken) { - if ($tokens->isCurrentTokenType($disallowedStartToken)) { - $tokens->consumeTokenType(Lexer::TOKEN_OTHER); // will throw exception + if (!$tokens->isCurrentTokenType($disallowedStartToken)) { + continue; } + + $tokens->consumeTokenType(Lexer::TOKEN_OTHER); // will throw exception } } diff --git a/src/Parser/TokenIterator.php b/src/Parser/TokenIterator.php index 5b3bfbd..b3f2208 100644 --- a/src/Parser/TokenIterator.php +++ b/src/Parser/TokenIterator.php @@ -21,9 +21,11 @@ class TokenIterator $this->tokens = $tokens; $this->index = $index; - if ($this->tokens[$this->index][Lexer::TYPE_OFFSET] === Lexer::TOKEN_HORIZONTAL_WS) { - $this->index++; + if ($this->tokens[$this->index][Lexer::TYPE_OFFSET] !== Lexer::TOKEN_HORIZONTAL_WS) { + return; } + + $this->index++; } @@ -66,7 +68,7 @@ class TokenIterator * @param int $tokenType * @throws \PHPStan\PhpDocParser\Parser\ParserException */ - public function consumeTokenType(int $tokenType) + public function consumeTokenType(int $tokenType): void { if ($this->tokens[$this->index][Lexer::TYPE_OFFSET] !== $tokenType) { $this->throwError($tokenType); @@ -74,9 +76,11 @@ class TokenIterator $this->index++; - if (($this->tokens[$this->index][Lexer::TYPE_OFFSET] ?? -1) === Lexer::TOKEN_HORIZONTAL_WS) { - $this->index++; + if (($this->tokens[$this->index][Lexer::TYPE_OFFSET] ?? -1) !== Lexer::TOKEN_HORIZONTAL_WS) { + return; } + + $this->index++; } @@ -132,29 +136,31 @@ class TokenIterator } - public function next() + public function next(): void { $this->index++; - if ($this->tokens[$this->index][Lexer::TYPE_OFFSET] === Lexer::TOKEN_HORIZONTAL_WS) { - $this->index++; + if ($this->tokens[$this->index][Lexer::TYPE_OFFSET] !== Lexer::TOKEN_HORIZONTAL_WS) { + return; } + + $this->index++; } - public function pushSavePoint() + public function pushSavePoint(): void { $this->savePoints[] = $this->index; } - public function dropSavePoint() + public function dropSavePoint(): void { array_pop($this->savePoints); } - public function rollback() + public function rollback(): void { $index = array_pop($this->savePoints); assert($index !== null); @@ -166,7 +172,7 @@ class TokenIterator * @param int $expectedTokenType * @throws \PHPStan\PhpDocParser\Parser\ParserException */ - private function throwError(int $expectedTokenType) + private function throwError(int $expectedTokenType): void { throw new \PHPStan\PhpDocParser\Parser\ParserException( $this->currentTokenValue(), diff --git a/tests/PHPStan/Parser/ConstExprParserTest.php b/tests/PHPStan/Parser/ConstExprParserTest.php index 7954214..425882f 100644 --- a/tests/PHPStan/Parser/ConstExprParserTest.php +++ b/tests/PHPStan/Parser/ConstExprParserTest.php @@ -24,7 +24,7 @@ class ConstExprParserTest extends \PHPUnit\Framework\TestCase /** @var ConstExprParser */ private $constExprParser; - protected function setUp() + protected function setUp(): void { parent::setUp(); $this->lexer = new Lexer(); @@ -45,7 +45,7 @@ class ConstExprParserTest extends \PHPUnit\Framework\TestCase * @param ConstExprNode $expectedExpr * @param int $nextTokenType */ - public function testParse(string $input, ConstExprNode $expectedExpr, int $nextTokenType = Lexer::TOKEN_END) + public function testParse(string $input, ConstExprNode $expectedExpr, int $nextTokenType = Lexer::TOKEN_END): void { $tokens = new TokenIterator($this->lexer->tokenize($input)); $exprNode = $this->constExprParser->parse($tokens); diff --git a/tests/PHPStan/Parser/FuzzyTest.php b/tests/PHPStan/Parser/FuzzyTest.php index a24c724..a850576 100644 --- a/tests/PHPStan/Parser/FuzzyTest.php +++ b/tests/PHPStan/Parser/FuzzyTest.php @@ -18,7 +18,7 @@ class FuzzyTest extends \PHPUnit\Framework\TestCase /** @var ConstExprParser */ private $constExprParser; - protected function setUp() + protected function setUp(): void { parent::setUp(); $this->lexer = new Lexer(); @@ -30,7 +30,7 @@ class FuzzyTest extends \PHPUnit\Framework\TestCase * @dataProvider provideTypeParserData * @param string $input */ - public function testTypeParser(string $input) + public function testTypeParser(string $input): void { $tokens = new TokenIterator($this->lexer->tokenize($input)); $this->typeParser->parse($tokens); @@ -51,7 +51,7 @@ class FuzzyTest extends \PHPUnit\Framework\TestCase * @dataProvider provideConstExprParserData * @param string $input */ - public function testConstExprParser(string $input) + public function testConstExprParser(string $input): void { $tokens = new TokenIterator($this->lexer->tokenize($input)); $this->constExprParser->parse($tokens); diff --git a/tests/PHPStan/Parser/PhpDocParserTest.php b/tests/PHPStan/Parser/PhpDocParserTest.php index 19f0cff..d5d0df5 100644 --- a/tests/PHPStan/Parser/PhpDocParserTest.php +++ b/tests/PHPStan/Parser/PhpDocParserTest.php @@ -30,7 +30,7 @@ class PhpDocParserTest extends \PHPUnit\Framework\TestCase /** @var PhpDocParser */ private $phpDocParser; - protected function setUp() + protected function setUp(): void { parent::setUp(); $this->lexer = new Lexer(); @@ -52,7 +52,7 @@ class PhpDocParserTest extends \PHPUnit\Framework\TestCase * @param PhpDocNode $expectedPhpDocNode * @param int $nextTokenType */ - public function testParse(string $label, string $input, PhpDocNode $expectedPhpDocNode, int $nextTokenType = Lexer::TOKEN_END) + public function testParse(string $label, string $input, PhpDocNode $expectedPhpDocNode, int $nextTokenType = Lexer::TOKEN_END): void { $tokens = new TokenIterator($this->lexer->tokenize($input)); $actualPhpDocNode = $this->phpDocParser->parse($tokens); @@ -1881,9 +1881,9 @@ class PhpDocParserTest extends \PHPUnit\Framework\TestCase new MethodTagValueNode( true, new UnionTypeNode([ - new IdentifierTypeNode('self'), - new IdentifierTypeNode('Bar'), - ]), + new IdentifierTypeNode('self'), + new IdentifierTypeNode('Bar'), + ]), 'getFooOrBarStaticallyWithDescription', [], 'Get a Foo or a Bar with a description statically.' @@ -1944,9 +1944,9 @@ class PhpDocParserTest extends \PHPUnit\Framework\TestCase new MethodTagValueNode( false, new UnionTypeNode([ - new IdentifierTypeNode('self'), - new IdentifierTypeNode('Bar'), - ]), + new IdentifierTypeNode('self'), + new IdentifierTypeNode('Bar'), + ]), 'getFooOrBarNoParams', [], '' @@ -1987,9 +1987,9 @@ class PhpDocParserTest extends \PHPUnit\Framework\TestCase new MethodTagValueNode( true, new UnionTypeNode([ - new IdentifierTypeNode('self'), - new IdentifierTypeNode('Bar'), - ]), + new IdentifierTypeNode('self'), + new IdentifierTypeNode('Bar'), + ]), 'getFooOrBarStaticallyNoParams', [], '' @@ -2030,9 +2030,9 @@ class PhpDocParserTest extends \PHPUnit\Framework\TestCase new MethodTagValueNode( false, new UnionTypeNode([ - new IdentifierTypeNode('self'), - new IdentifierTypeNode('Bar'), - ]), + new IdentifierTypeNode('self'), + new IdentifierTypeNode('Bar'), + ]), 'getFooOrBarWithDescriptionNoParams', [], 'Get a Foo or a Bar with a description.' @@ -2063,9 +2063,9 @@ class PhpDocParserTest extends \PHPUnit\Framework\TestCase new MethodTagValueNode( true, new UnionTypeNode([ - new IdentifierTypeNode('self'), - new IdentifierTypeNode('Bar'), - ]), + new IdentifierTypeNode('self'), + new IdentifierTypeNode('Bar'), + ]), 'getFooOrBarStaticallyWithDescriptionNoParams', [], 'Get a Foo or a Bar with a description statically.' @@ -2076,9 +2076,9 @@ class PhpDocParserTest extends \PHPUnit\Framework\TestCase new MethodTagValueNode( true, new UnionTypeNode([ - new IdentifierTypeNode('bool'), - new IdentifierTypeNode('string'), - ]), + new IdentifierTypeNode('bool'), + new IdentifierTypeNode('string'), + ]), 'aStaticMethodThatHasAUniqueReturnTypeInThisClassNoParams', [], '' @@ -2089,9 +2089,9 @@ class PhpDocParserTest extends \PHPUnit\Framework\TestCase new MethodTagValueNode( true, new UnionTypeNode([ - new IdentifierTypeNode('string'), - new IdentifierTypeNode('float'), - ]), + new IdentifierTypeNode('string'), + new IdentifierTypeNode('float'), + ]), 'aStaticMethodThatHasAUniqueReturnTypeInThisClassWithDescriptionNoParams', [], 'A Description.' diff --git a/tests/PHPStan/Parser/TypeParserTest.php b/tests/PHPStan/Parser/TypeParserTest.php index 00b8b6d..da5852e 100644 --- a/tests/PHPStan/Parser/TypeParserTest.php +++ b/tests/PHPStan/Parser/TypeParserTest.php @@ -23,7 +23,7 @@ class TypeParserTest extends \PHPUnit\Framework\TestCase /** @var TypeParser */ private $typeParser; - protected function setUp() + protected function setUp(): void { parent::setUp(); $this->lexer = new Lexer(); @@ -37,7 +37,7 @@ class TypeParserTest extends \PHPUnit\Framework\TestCase * @param TypeNode $expectedType * @param int $nextTokenType */ - public function testParse(string $input, TypeNode $expectedType, int $nextTokenType = Lexer::TOKEN_END) + public function testParse(string $input, TypeNode $expectedType, int $nextTokenType = Lexer::TOKEN_END): void { $tokens = new TokenIterator($this->lexer->tokenize($input)); $typeNode = $this->typeParser->parse($tokens);