Updated coding standard

This commit is contained in:
Ondrej Mirtes 2019-04-23 21:27:03 +02:00
parent c46bef1f1b
commit a3dbd6c257
No known key found for this signature in database
GPG Key ID: 8E730BA25823D8B5
15 changed files with 119 additions and 88 deletions

View File

@ -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": {

View File

@ -1,21 +1,14 @@
<?xml version="1.0"?>
<ruleset name="PHPStan PHPDoc Parser">
<rule ref="vendor/consistence/coding-standard/Consistence/ruleset.xml">
<exclude name="Squiz.Functions.GlobalFunction.Found"/>
<exclude name="Squiz.Strings.DoubleQuoteUsage.ContainsVar"/>
</rule>
<rule ref="vendor/slevomat/coding-standard/SlevomatCodingStandard/ruleset.xml">
<exclude name="SlevomatCodingStandard.Classes.ClassConstantVisibility.MissingConstantVisibility"/>
<exclude name="SlevomatCodingStandard.Files.TypeNameMatchesFileName"/>
<exclude name="SlevomatCodingStandard.Commenting.InlineDocCommentDeclaration.InvalidFormat"/>
<exclude name="SlevomatCodingStandard.Namespaces.FullyQualifiedClassNameAfterKeyword"/>
<exclude name="SlevomatCodingStandard.Namespaces.UseOnlyWhitelistedNamespaces"/>
<exclude name="SlevomatCodingStandard.Namespaces.FullyQualifiedClassNameInAnnotation"/>
<exclude name="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly"/>
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableParameterTypeHintSpecification"/>
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableReturnTypeHintSpecification"/>
<exclude name="SlevomatCodingStandard.Namespaces.FullyQualifiedClassNameInAnnotation.NonFullyQualifiedClassName"/>
<exclude name="SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalConstants"/>
<exclude name="SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalFunctions"/>
<exclude name="SlevomatCodingStandard.TypeHints.NullableTypeForNullDefaultValue"/>
<exclude name="Consistence.Exceptions.ExceptionDeclaration"/>
<exclude name="Squiz.Commenting.FunctionComment.MissingParamTag"/>
<exclude name="Squiz.Commenting.FunctionComment.ParamNameNoMatch"/>
<exclude name="Squiz.Strings.DoubleQuoteUsage.ContainsVar"/>
</rule>
<rule ref="SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses">
<properties>
@ -31,11 +24,40 @@
<properties>
<property name="usefulAnnotations" type="array" value="
@dataProvider,
@requires,
@throws
@requires
"/>
<property name="enableNullableTypeHints" type="false" />
<property name="enableVoidTypeHint" type="false" />
<property name="enableObjectTypeHint" value="false"/>
</properties>
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableParameterTypeHintSpecification"/>
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableReturnTypeHintSpecification"/>
</rule>
<rule ref="SlevomatCodingStandard.ControlStructures.AssignmentInCondition"/>
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowEqualOperators"/>
<rule ref="SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator"/>
<rule ref="SlevomatCodingStandard.ControlStructures.EarlyExit"/>
<rule ref="SlevomatCodingStandard.Classes.SuperfluousAbstractClassNaming"/>
<rule ref="SlevomatCodingStandard.Classes.SuperfluousInterfaceNaming"/>
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowShortTernaryOperator"/>
<rule ref="SlevomatCodingStandard.Files.TypeNameMatchesFileName">
<properties>
<property name="rootNamespaces" type="array" value="src=>PHPStan\PhpDocParser,tests/PHPStan=>PHPStan\PhpDocParser"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.Classes.ModernClassNameReference"/>
<rule ref="SlevomatCodingStandard.Functions.StaticClosure"/>
<rule ref="SlevomatCodingStandard.Operators.RequireCombinedAssignmentOperator"/>
<rule ref="SlevomatCodingStandard.TypeHints.NullTypeHintOnLastPosition"/>
<rule ref="SlevomatCodingStandard.Classes.TraitUseDeclaration"/>
<rule ref="SlevomatCodingStandard.Classes.TraitUseSpacing"/>
<rule ref="SlevomatCodingStandard.Variables.UnusedVariable"/>
<rule ref="SlevomatCodingStandard.Variables.UselessVariable"/>
<!--<rule ref="SlevomatCodingStandard.Functions.UnusedParameter"/>-->
<rule ref="SlevomatCodingStandard.Functions.UnusedInheritedVariablePassedToClosure"/>
<rule ref="SlevomatCodingStandard.Namespaces.UselessAlias"/>
<rule ref="SlevomatCodingStandard.PHP.UselessSemicolon"/>
<rule ref="SlevomatCodingStandard.PHP.UselessParentheses"/>
<exclude-pattern>tests/*/data</exclude-pattern>
<exclude-pattern>tests/*/traits</exclude-pattern>
<exclude-pattern>tests/notAutoloaded</exclude-pattern>
<exclude-pattern>src/Reflection/SignatureMap/functionMap.php</exclude-pattern>
</ruleset>

View File

@ -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}";
}
}

View File

@ -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}";
}
}

View File

@ -10,7 +10,7 @@ class MethodTagValueNode implements PhpDocTagValueNode
/** @var bool */
public $isStatic;
/** @var null|TypeNode */
/** @var TypeNode|null */
public $returnType;
/** @var string */

View File

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

View File

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

View File

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

View File

@ -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);
}

View File

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

View File

@ -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(),

View File

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

View File

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

View File

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

View File

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