From d9ce9f6336e9767e72c5c1dc92061b00d7d4ae64 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Sun, 7 Feb 2021 19:08:48 +0100 Subject: [PATCH 1/4] add AbstractNode with attributes and make all nodes extend it --- src/Ast/BaseNode.php | 37 +++++++++++++++++++ src/Ast/ConstExpr/ConstExprArrayItemNode.php | 4 +- src/Ast/ConstExpr/ConstExprArrayNode.php | 4 +- src/Ast/ConstExpr/ConstExprFalseNode.php | 4 +- src/Ast/ConstExpr/ConstExprFloatNode.php | 4 +- src/Ast/ConstExpr/ConstExprIntegerNode.php | 4 +- src/Ast/ConstExpr/ConstExprNullNode.php | 4 +- src/Ast/ConstExpr/ConstExprStringNode.php | 4 +- src/Ast/ConstExpr/ConstExprTrueNode.php | 4 +- src/Ast/ConstExpr/ConstFetchNode.php | 4 +- src/Ast/PhpDoc/DeprecatedTagValueNode.php | 4 +- src/Ast/PhpDoc/ExtendsTagValueNode.php | 3 +- src/Ast/PhpDoc/GenericTagValueNode.php | 4 +- src/Ast/PhpDoc/ImplementsTagValueNode.php | 3 +- src/Ast/PhpDoc/InvalidTagValueNode.php | 4 +- src/Ast/PhpDoc/MethodTagValueNode.php | 3 +- .../PhpDoc/MethodTagValueParameterNode.php | 3 +- src/Ast/PhpDoc/MixinTagValueNode.php | 3 +- src/Ast/PhpDoc/ParamTagValueNode.php | 3 +- src/Ast/PhpDoc/PhpDocNode.php | 3 +- src/Ast/PhpDoc/PhpDocTagNode.php | 4 +- src/Ast/PhpDoc/PhpDocTextNode.php | 4 +- src/Ast/PhpDoc/PropertyTagValueNode.php | 3 +- src/Ast/PhpDoc/ReturnTagValueNode.php | 3 +- src/Ast/PhpDoc/TemplateTagValueNode.php | 3 +- src/Ast/PhpDoc/ThrowsTagValueNode.php | 3 +- src/Ast/PhpDoc/UsesTagValueNode.php | 3 +- src/Ast/PhpDoc/VarTagValueNode.php | 3 +- src/Ast/Type/ArrayShapeItemNode.php | 3 +- src/Ast/Type/ArrayShapeNode.php | 4 +- src/Ast/Type/ArrayTypeNode.php | 4 +- src/Ast/Type/CallableTypeNode.php | 4 +- src/Ast/Type/CallableTypeParameterNode.php | 3 +- src/Ast/Type/ConstTypeNode.php | 3 +- src/Ast/Type/GenericTypeNode.php | 4 +- src/Ast/Type/IdentifierTypeNode.php | 4 +- src/Ast/Type/IntersectionTypeNode.php | 4 +- src/Ast/Type/NullableTypeNode.php | 4 +- src/Ast/Type/ThisTypeNode.php | 4 +- src/Ast/Type/UnionTypeNode.php | 4 +- .../PHPStan/Ast/Attributes/AttributesTest.php | 32 ++++++++++++++++ 41 files changed, 170 insertions(+), 39 deletions(-) create mode 100644 src/Ast/BaseNode.php create mode 100644 tests/PHPStan/Ast/Attributes/AttributesTest.php diff --git a/src/Ast/BaseNode.php b/src/Ast/BaseNode.php new file mode 100644 index 00000000..05b540ea --- /dev/null +++ b/src/Ast/BaseNode.php @@ -0,0 +1,37 @@ + */ + private $attributes = []; + + /** + * @param mixed $value + */ + public function setAttribute(string $key, $value): void + { + $this->attributes[$key] = $value; + } + + public function hasAttribute(string $key): bool + { + return array_key_exists($key, $this->attributes); + } + + /** + * @param mixed|null $default + * @return mixed|null + */ + public function getAttribute(string $key, $default = null) + { + if ($this->hasAttribute($key)) { + return $this->attributes[$key]; + } + + return $default; + } + +} diff --git a/src/Ast/ConstExpr/ConstExprArrayItemNode.php b/src/Ast/ConstExpr/ConstExprArrayItemNode.php index 9456e95d..16662026 100644 --- a/src/Ast/ConstExpr/ConstExprArrayItemNode.php +++ b/src/Ast/ConstExpr/ConstExprArrayItemNode.php @@ -2,7 +2,9 @@ namespace PHPStan\PhpDocParser\Ast\ConstExpr; -class ConstExprArrayItemNode implements ConstExprNode +use PHPStan\PhpDocParser\Ast\BaseNode; + +class ConstExprArrayItemNode extends BaseNode implements ConstExprNode { /** @var ConstExprNode|null */ diff --git a/src/Ast/ConstExpr/ConstExprArrayNode.php b/src/Ast/ConstExpr/ConstExprArrayNode.php index 215c5a37..ec5de3a4 100644 --- a/src/Ast/ConstExpr/ConstExprArrayNode.php +++ b/src/Ast/ConstExpr/ConstExprArrayNode.php @@ -2,7 +2,9 @@ namespace PHPStan\PhpDocParser\Ast\ConstExpr; -class ConstExprArrayNode implements ConstExprNode +use PHPStan\PhpDocParser\Ast\BaseNode; + +class ConstExprArrayNode extends BaseNode implements ConstExprNode { /** @var ConstExprArrayItemNode[] */ diff --git a/src/Ast/ConstExpr/ConstExprFalseNode.php b/src/Ast/ConstExpr/ConstExprFalseNode.php index fae8af1b..496d11bc 100644 --- a/src/Ast/ConstExpr/ConstExprFalseNode.php +++ b/src/Ast/ConstExpr/ConstExprFalseNode.php @@ -2,7 +2,9 @@ namespace PHPStan\PhpDocParser\Ast\ConstExpr; -class ConstExprFalseNode implements ConstExprNode +use PHPStan\PhpDocParser\Ast\BaseNode; + +class ConstExprFalseNode extends BaseNode implements ConstExprNode { public function __toString(): string diff --git a/src/Ast/ConstExpr/ConstExprFloatNode.php b/src/Ast/ConstExpr/ConstExprFloatNode.php index 0f81c418..96a3081d 100644 --- a/src/Ast/ConstExpr/ConstExprFloatNode.php +++ b/src/Ast/ConstExpr/ConstExprFloatNode.php @@ -2,7 +2,9 @@ namespace PHPStan\PhpDocParser\Ast\ConstExpr; -class ConstExprFloatNode implements ConstExprNode +use PHPStan\PhpDocParser\Ast\BaseNode; + +class ConstExprFloatNode extends BaseNode implements ConstExprNode { /** @var string */ diff --git a/src/Ast/ConstExpr/ConstExprIntegerNode.php b/src/Ast/ConstExpr/ConstExprIntegerNode.php index 949ba5cb..bcae3b3f 100644 --- a/src/Ast/ConstExpr/ConstExprIntegerNode.php +++ b/src/Ast/ConstExpr/ConstExprIntegerNode.php @@ -2,7 +2,9 @@ namespace PHPStan\PhpDocParser\Ast\ConstExpr; -class ConstExprIntegerNode implements ConstExprNode +use PHPStan\PhpDocParser\Ast\BaseNode; + +class ConstExprIntegerNode extends BaseNode implements ConstExprNode { /** @var string */ diff --git a/src/Ast/ConstExpr/ConstExprNullNode.php b/src/Ast/ConstExpr/ConstExprNullNode.php index 1d51eb80..38c13999 100644 --- a/src/Ast/ConstExpr/ConstExprNullNode.php +++ b/src/Ast/ConstExpr/ConstExprNullNode.php @@ -2,7 +2,9 @@ namespace PHPStan\PhpDocParser\Ast\ConstExpr; -class ConstExprNullNode implements ConstExprNode +use PHPStan\PhpDocParser\Ast\BaseNode; + +class ConstExprNullNode extends BaseNode implements ConstExprNode { public function __toString(): string diff --git a/src/Ast/ConstExpr/ConstExprStringNode.php b/src/Ast/ConstExpr/ConstExprStringNode.php index d9c720a0..14c459bc 100644 --- a/src/Ast/ConstExpr/ConstExprStringNode.php +++ b/src/Ast/ConstExpr/ConstExprStringNode.php @@ -2,7 +2,9 @@ namespace PHPStan\PhpDocParser\Ast\ConstExpr; -class ConstExprStringNode implements ConstExprNode +use PHPStan\PhpDocParser\Ast\BaseNode; + +class ConstExprStringNode extends BaseNode implements ConstExprNode { /** @var string */ diff --git a/src/Ast/ConstExpr/ConstExprTrueNode.php b/src/Ast/ConstExpr/ConstExprTrueNode.php index b0e3c003..99d3d0d6 100644 --- a/src/Ast/ConstExpr/ConstExprTrueNode.php +++ b/src/Ast/ConstExpr/ConstExprTrueNode.php @@ -2,7 +2,9 @@ namespace PHPStan\PhpDocParser\Ast\ConstExpr; -class ConstExprTrueNode implements ConstExprNode +use PHPStan\PhpDocParser\Ast\BaseNode; + +class ConstExprTrueNode extends BaseNode implements ConstExprNode { public function __toString(): string diff --git a/src/Ast/ConstExpr/ConstFetchNode.php b/src/Ast/ConstExpr/ConstFetchNode.php index a432741d..3fe161ce 100644 --- a/src/Ast/ConstExpr/ConstFetchNode.php +++ b/src/Ast/ConstExpr/ConstFetchNode.php @@ -2,7 +2,9 @@ namespace PHPStan\PhpDocParser\Ast\ConstExpr; -class ConstFetchNode implements ConstExprNode +use PHPStan\PhpDocParser\Ast\BaseNode; + +class ConstFetchNode extends BaseNode implements ConstExprNode { /** @var string class name for class constants or empty string for non-class constants */ diff --git a/src/Ast/PhpDoc/DeprecatedTagValueNode.php b/src/Ast/PhpDoc/DeprecatedTagValueNode.php index 315d902b..02509b60 100644 --- a/src/Ast/PhpDoc/DeprecatedTagValueNode.php +++ b/src/Ast/PhpDoc/DeprecatedTagValueNode.php @@ -2,7 +2,9 @@ namespace PHPStan\PhpDocParser\Ast\PhpDoc; -class DeprecatedTagValueNode implements PhpDocTagValueNode +use PHPStan\PhpDocParser\Ast\BaseNode; + +class DeprecatedTagValueNode extends BaseNode implements PhpDocTagValueNode { /** @var string (may be empty) */ diff --git a/src/Ast/PhpDoc/ExtendsTagValueNode.php b/src/Ast/PhpDoc/ExtendsTagValueNode.php index 513f2975..f938407f 100644 --- a/src/Ast/PhpDoc/ExtendsTagValueNode.php +++ b/src/Ast/PhpDoc/ExtendsTagValueNode.php @@ -2,9 +2,10 @@ namespace PHPStan\PhpDocParser\Ast\PhpDoc; +use PHPStan\PhpDocParser\Ast\BaseNode; use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode; -class ExtendsTagValueNode implements PhpDocTagValueNode +class ExtendsTagValueNode extends BaseNode implements PhpDocTagValueNode { /** @var GenericTypeNode */ diff --git a/src/Ast/PhpDoc/GenericTagValueNode.php b/src/Ast/PhpDoc/GenericTagValueNode.php index 97518ab6..eaf681fa 100644 --- a/src/Ast/PhpDoc/GenericTagValueNode.php +++ b/src/Ast/PhpDoc/GenericTagValueNode.php @@ -2,7 +2,9 @@ namespace PHPStan\PhpDocParser\Ast\PhpDoc; -class GenericTagValueNode implements PhpDocTagValueNode +use PHPStan\PhpDocParser\Ast\BaseNode; + +class GenericTagValueNode extends BaseNode implements PhpDocTagValueNode { /** @var string (may be empty) */ diff --git a/src/Ast/PhpDoc/ImplementsTagValueNode.php b/src/Ast/PhpDoc/ImplementsTagValueNode.php index 7691d93a..3fb88055 100644 --- a/src/Ast/PhpDoc/ImplementsTagValueNode.php +++ b/src/Ast/PhpDoc/ImplementsTagValueNode.php @@ -2,9 +2,10 @@ namespace PHPStan\PhpDocParser\Ast\PhpDoc; +use PHPStan\PhpDocParser\Ast\BaseNode; use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode; -class ImplementsTagValueNode implements PhpDocTagValueNode +class ImplementsTagValueNode extends BaseNode implements PhpDocTagValueNode { /** @var GenericTypeNode */ diff --git a/src/Ast/PhpDoc/InvalidTagValueNode.php b/src/Ast/PhpDoc/InvalidTagValueNode.php index 150acae5..bd52f106 100644 --- a/src/Ast/PhpDoc/InvalidTagValueNode.php +++ b/src/Ast/PhpDoc/InvalidTagValueNode.php @@ -2,7 +2,9 @@ namespace PHPStan\PhpDocParser\Ast\PhpDoc; -class InvalidTagValueNode implements PhpDocTagValueNode +use PHPStan\PhpDocParser\Ast\BaseNode; + +class InvalidTagValueNode extends BaseNode implements PhpDocTagValueNode { /** @var string (may be empty) */ diff --git a/src/Ast/PhpDoc/MethodTagValueNode.php b/src/Ast/PhpDoc/MethodTagValueNode.php index 8f5035c3..401b2482 100644 --- a/src/Ast/PhpDoc/MethodTagValueNode.php +++ b/src/Ast/PhpDoc/MethodTagValueNode.php @@ -2,9 +2,10 @@ namespace PHPStan\PhpDocParser\Ast\PhpDoc; +use PHPStan\PhpDocParser\Ast\BaseNode; use PHPStan\PhpDocParser\Ast\Type\TypeNode; -class MethodTagValueNode implements PhpDocTagValueNode +class MethodTagValueNode extends BaseNode implements PhpDocTagValueNode { /** @var bool */ diff --git a/src/Ast/PhpDoc/MethodTagValueParameterNode.php b/src/Ast/PhpDoc/MethodTagValueParameterNode.php index f7409f3e..c399db7e 100644 --- a/src/Ast/PhpDoc/MethodTagValueParameterNode.php +++ b/src/Ast/PhpDoc/MethodTagValueParameterNode.php @@ -2,11 +2,12 @@ namespace PHPStan\PhpDocParser\Ast\PhpDoc; +use PHPStan\PhpDocParser\Ast\BaseNode; use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprNode; use PHPStan\PhpDocParser\Ast\Node; use PHPStan\PhpDocParser\Ast\Type\TypeNode; -class MethodTagValueParameterNode implements Node +class MethodTagValueParameterNode extends BaseNode implements Node { /** @var TypeNode|null */ diff --git a/src/Ast/PhpDoc/MixinTagValueNode.php b/src/Ast/PhpDoc/MixinTagValueNode.php index 8290181d..14c9c925 100644 --- a/src/Ast/PhpDoc/MixinTagValueNode.php +++ b/src/Ast/PhpDoc/MixinTagValueNode.php @@ -2,9 +2,10 @@ namespace PHPStan\PhpDocParser\Ast\PhpDoc; +use PHPStan\PhpDocParser\Ast\BaseNode; use PHPStan\PhpDocParser\Ast\Type\TypeNode; -class MixinTagValueNode implements PhpDocTagValueNode +class MixinTagValueNode extends BaseNode implements PhpDocTagValueNode { /** @var TypeNode */ diff --git a/src/Ast/PhpDoc/ParamTagValueNode.php b/src/Ast/PhpDoc/ParamTagValueNode.php index 04710a7b..78f3233c 100644 --- a/src/Ast/PhpDoc/ParamTagValueNode.php +++ b/src/Ast/PhpDoc/ParamTagValueNode.php @@ -2,9 +2,10 @@ namespace PHPStan\PhpDocParser\Ast\PhpDoc; +use PHPStan\PhpDocParser\Ast\BaseNode; use PHPStan\PhpDocParser\Ast\Type\TypeNode; -class ParamTagValueNode implements PhpDocTagValueNode +class ParamTagValueNode extends BaseNode implements PhpDocTagValueNode { /** @var TypeNode */ diff --git a/src/Ast/PhpDoc/PhpDocNode.php b/src/Ast/PhpDoc/PhpDocNode.php index ace03b1f..2cfbe6fd 100644 --- a/src/Ast/PhpDoc/PhpDocNode.php +++ b/src/Ast/PhpDoc/PhpDocNode.php @@ -2,9 +2,10 @@ namespace PHPStan\PhpDocParser\Ast\PhpDoc; +use PHPStan\PhpDocParser\Ast\BaseNode; use PHPStan\PhpDocParser\Ast\Node; -class PhpDocNode implements Node +class PhpDocNode extends BaseNode implements Node { /** @var PhpDocChildNode[] */ diff --git a/src/Ast/PhpDoc/PhpDocTagNode.php b/src/Ast/PhpDoc/PhpDocTagNode.php index be3043bf..169d669b 100644 --- a/src/Ast/PhpDoc/PhpDocTagNode.php +++ b/src/Ast/PhpDoc/PhpDocTagNode.php @@ -2,7 +2,9 @@ namespace PHPStan\PhpDocParser\Ast\PhpDoc; -class PhpDocTagNode implements PhpDocChildNode +use PHPStan\PhpDocParser\Ast\BaseNode; + +class PhpDocTagNode extends BaseNode implements PhpDocChildNode { /** @var string */ diff --git a/src/Ast/PhpDoc/PhpDocTextNode.php b/src/Ast/PhpDoc/PhpDocTextNode.php index 2a4f8a4c..a52ffdaf 100644 --- a/src/Ast/PhpDoc/PhpDocTextNode.php +++ b/src/Ast/PhpDoc/PhpDocTextNode.php @@ -2,7 +2,9 @@ namespace PHPStan\PhpDocParser\Ast\PhpDoc; -class PhpDocTextNode implements PhpDocChildNode +use PHPStan\PhpDocParser\Ast\BaseNode; + +class PhpDocTextNode extends BaseNode implements PhpDocChildNode { /** @var string */ diff --git a/src/Ast/PhpDoc/PropertyTagValueNode.php b/src/Ast/PhpDoc/PropertyTagValueNode.php index bca55a7e..12e9ef4a 100644 --- a/src/Ast/PhpDoc/PropertyTagValueNode.php +++ b/src/Ast/PhpDoc/PropertyTagValueNode.php @@ -2,9 +2,10 @@ namespace PHPStan\PhpDocParser\Ast\PhpDoc; +use PHPStan\PhpDocParser\Ast\BaseNode; use PHPStan\PhpDocParser\Ast\Type\TypeNode; -class PropertyTagValueNode implements PhpDocTagValueNode +class PropertyTagValueNode extends BaseNode implements PhpDocTagValueNode { /** @var TypeNode */ diff --git a/src/Ast/PhpDoc/ReturnTagValueNode.php b/src/Ast/PhpDoc/ReturnTagValueNode.php index 254d3f87..34cfad4f 100644 --- a/src/Ast/PhpDoc/ReturnTagValueNode.php +++ b/src/Ast/PhpDoc/ReturnTagValueNode.php @@ -2,9 +2,10 @@ namespace PHPStan\PhpDocParser\Ast\PhpDoc; +use PHPStan\PhpDocParser\Ast\BaseNode; use PHPStan\PhpDocParser\Ast\Type\TypeNode; -class ReturnTagValueNode implements PhpDocTagValueNode +class ReturnTagValueNode extends BaseNode implements PhpDocTagValueNode { /** @var TypeNode */ diff --git a/src/Ast/PhpDoc/TemplateTagValueNode.php b/src/Ast/PhpDoc/TemplateTagValueNode.php index 5847df22..addfc453 100644 --- a/src/Ast/PhpDoc/TemplateTagValueNode.php +++ b/src/Ast/PhpDoc/TemplateTagValueNode.php @@ -2,9 +2,10 @@ namespace PHPStan\PhpDocParser\Ast\PhpDoc; +use PHPStan\PhpDocParser\Ast\BaseNode; use PHPStan\PhpDocParser\Ast\Type\TypeNode; -class TemplateTagValueNode implements PhpDocTagValueNode +class TemplateTagValueNode extends BaseNode implements PhpDocTagValueNode { /** @var string */ diff --git a/src/Ast/PhpDoc/ThrowsTagValueNode.php b/src/Ast/PhpDoc/ThrowsTagValueNode.php index 32ae12d2..f05d82f7 100644 --- a/src/Ast/PhpDoc/ThrowsTagValueNode.php +++ b/src/Ast/PhpDoc/ThrowsTagValueNode.php @@ -2,9 +2,10 @@ namespace PHPStan\PhpDocParser\Ast\PhpDoc; +use PHPStan\PhpDocParser\Ast\BaseNode; use PHPStan\PhpDocParser\Ast\Type\TypeNode; -class ThrowsTagValueNode implements PhpDocTagValueNode +class ThrowsTagValueNode extends BaseNode implements PhpDocTagValueNode { /** @var TypeNode */ diff --git a/src/Ast/PhpDoc/UsesTagValueNode.php b/src/Ast/PhpDoc/UsesTagValueNode.php index 20b19390..ee4b290c 100644 --- a/src/Ast/PhpDoc/UsesTagValueNode.php +++ b/src/Ast/PhpDoc/UsesTagValueNode.php @@ -2,9 +2,10 @@ namespace PHPStan\PhpDocParser\Ast\PhpDoc; +use PHPStan\PhpDocParser\Ast\BaseNode; use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode; -class UsesTagValueNode implements PhpDocTagValueNode +class UsesTagValueNode extends BaseNode implements PhpDocTagValueNode { /** @var GenericTypeNode */ diff --git a/src/Ast/PhpDoc/VarTagValueNode.php b/src/Ast/PhpDoc/VarTagValueNode.php index 2965ce7c..54b74692 100644 --- a/src/Ast/PhpDoc/VarTagValueNode.php +++ b/src/Ast/PhpDoc/VarTagValueNode.php @@ -2,9 +2,10 @@ namespace PHPStan\PhpDocParser\Ast\PhpDoc; +use PHPStan\PhpDocParser\Ast\BaseNode; use PHPStan\PhpDocParser\Ast\Type\TypeNode; -class VarTagValueNode implements PhpDocTagValueNode +class VarTagValueNode extends BaseNode implements PhpDocTagValueNode { /** @var TypeNode */ diff --git a/src/Ast/Type/ArrayShapeItemNode.php b/src/Ast/Type/ArrayShapeItemNode.php index b4996e90..58444873 100644 --- a/src/Ast/Type/ArrayShapeItemNode.php +++ b/src/Ast/Type/ArrayShapeItemNode.php @@ -2,10 +2,11 @@ namespace PHPStan\PhpDocParser\Ast\Type; +use PHPStan\PhpDocParser\Ast\BaseNode; use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode; use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprStringNode; -class ArrayShapeItemNode implements TypeNode +class ArrayShapeItemNode extends BaseNode implements TypeNode { /** @var ConstExprIntegerNode|ConstExprStringNode|IdentifierTypeNode|null */ diff --git a/src/Ast/Type/ArrayShapeNode.php b/src/Ast/Type/ArrayShapeNode.php index 74df4ab3..23590ec6 100644 --- a/src/Ast/Type/ArrayShapeNode.php +++ b/src/Ast/Type/ArrayShapeNode.php @@ -2,7 +2,9 @@ namespace PHPStan\PhpDocParser\Ast\Type; -class ArrayShapeNode implements TypeNode +use PHPStan\PhpDocParser\Ast\BaseNode; + +class ArrayShapeNode extends BaseNode implements TypeNode { /** @var ArrayShapeItemNode[] */ diff --git a/src/Ast/Type/ArrayTypeNode.php b/src/Ast/Type/ArrayTypeNode.php index b01d083c..97e0834e 100644 --- a/src/Ast/Type/ArrayTypeNode.php +++ b/src/Ast/Type/ArrayTypeNode.php @@ -2,7 +2,9 @@ namespace PHPStan\PhpDocParser\Ast\Type; -class ArrayTypeNode implements TypeNode +use PHPStan\PhpDocParser\Ast\BaseNode; + +class ArrayTypeNode extends BaseNode implements TypeNode { /** @var TypeNode */ diff --git a/src/Ast/Type/CallableTypeNode.php b/src/Ast/Type/CallableTypeNode.php index 2f4bf7c5..10313806 100644 --- a/src/Ast/Type/CallableTypeNode.php +++ b/src/Ast/Type/CallableTypeNode.php @@ -2,7 +2,9 @@ namespace PHPStan\PhpDocParser\Ast\Type; -class CallableTypeNode implements TypeNode +use PHPStan\PhpDocParser\Ast\BaseNode; + +class CallableTypeNode extends BaseNode implements TypeNode { /** @var IdentifierTypeNode */ diff --git a/src/Ast/Type/CallableTypeParameterNode.php b/src/Ast/Type/CallableTypeParameterNode.php index 3503318d..c17420d7 100644 --- a/src/Ast/Type/CallableTypeParameterNode.php +++ b/src/Ast/Type/CallableTypeParameterNode.php @@ -2,9 +2,10 @@ namespace PHPStan\PhpDocParser\Ast\Type; +use PHPStan\PhpDocParser\Ast\BaseNode; use PHPStan\PhpDocParser\Ast\Node; -class CallableTypeParameterNode implements Node +class CallableTypeParameterNode extends BaseNode implements Node { /** @var TypeNode */ diff --git a/src/Ast/Type/ConstTypeNode.php b/src/Ast/Type/ConstTypeNode.php index ee9f14da..4333bdac 100644 --- a/src/Ast/Type/ConstTypeNode.php +++ b/src/Ast/Type/ConstTypeNode.php @@ -2,9 +2,10 @@ namespace PHPStan\PhpDocParser\Ast\Type; +use PHPStan\PhpDocParser\Ast\BaseNode; use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprNode; -class ConstTypeNode implements TypeNode +class ConstTypeNode extends BaseNode implements TypeNode { /** @var ConstExprNode */ diff --git a/src/Ast/Type/GenericTypeNode.php b/src/Ast/Type/GenericTypeNode.php index 4dcc9c8f..39aa49c0 100644 --- a/src/Ast/Type/GenericTypeNode.php +++ b/src/Ast/Type/GenericTypeNode.php @@ -2,7 +2,9 @@ namespace PHPStan\PhpDocParser\Ast\Type; -class GenericTypeNode implements TypeNode +use PHPStan\PhpDocParser\Ast\BaseNode; + +class GenericTypeNode extends BaseNode implements TypeNode { /** @var IdentifierTypeNode */ diff --git a/src/Ast/Type/IdentifierTypeNode.php b/src/Ast/Type/IdentifierTypeNode.php index 9ca389cc..903e6796 100644 --- a/src/Ast/Type/IdentifierTypeNode.php +++ b/src/Ast/Type/IdentifierTypeNode.php @@ -2,7 +2,9 @@ namespace PHPStan\PhpDocParser\Ast\Type; -class IdentifierTypeNode implements TypeNode +use PHPStan\PhpDocParser\Ast\BaseNode; + +class IdentifierTypeNode extends BaseNode implements TypeNode { /** @var string */ diff --git a/src/Ast/Type/IntersectionTypeNode.php b/src/Ast/Type/IntersectionTypeNode.php index 60341187..cdac1b19 100644 --- a/src/Ast/Type/IntersectionTypeNode.php +++ b/src/Ast/Type/IntersectionTypeNode.php @@ -2,7 +2,9 @@ namespace PHPStan\PhpDocParser\Ast\Type; -class IntersectionTypeNode implements TypeNode +use PHPStan\PhpDocParser\Ast\BaseNode; + +class IntersectionTypeNode extends BaseNode implements TypeNode { /** @var TypeNode[] */ diff --git a/src/Ast/Type/NullableTypeNode.php b/src/Ast/Type/NullableTypeNode.php index 98c73647..c384fca5 100644 --- a/src/Ast/Type/NullableTypeNode.php +++ b/src/Ast/Type/NullableTypeNode.php @@ -2,7 +2,9 @@ namespace PHPStan\PhpDocParser\Ast\Type; -class NullableTypeNode implements TypeNode +use PHPStan\PhpDocParser\Ast\BaseNode; + +class NullableTypeNode extends BaseNode implements TypeNode { /** @var TypeNode */ diff --git a/src/Ast/Type/ThisTypeNode.php b/src/Ast/Type/ThisTypeNode.php index 06a3537e..c026f76a 100644 --- a/src/Ast/Type/ThisTypeNode.php +++ b/src/Ast/Type/ThisTypeNode.php @@ -2,7 +2,9 @@ namespace PHPStan\PhpDocParser\Ast\Type; -class ThisTypeNode implements TypeNode +use PHPStan\PhpDocParser\Ast\BaseNode; + +class ThisTypeNode extends BaseNode implements TypeNode { public function __toString(): string diff --git a/src/Ast/Type/UnionTypeNode.php b/src/Ast/Type/UnionTypeNode.php index 235b6d5a..76bb07c9 100644 --- a/src/Ast/Type/UnionTypeNode.php +++ b/src/Ast/Type/UnionTypeNode.php @@ -2,7 +2,9 @@ namespace PHPStan\PhpDocParser\Ast\Type; -class UnionTypeNode implements TypeNode +use PHPStan\PhpDocParser\Ast\BaseNode; + +class UnionTypeNode extends BaseNode implements TypeNode { /** @var TypeNode[] */ diff --git a/tests/PHPStan/Ast/Attributes/AttributesTest.php b/tests/PHPStan/Ast/Attributes/AttributesTest.php new file mode 100644 index 00000000..ff92ccd5 --- /dev/null +++ b/tests/PHPStan/Ast/Attributes/AttributesTest.php @@ -0,0 +1,32 @@ +lexer = new Lexer(); + $constExprParser = new ConstExprParser(); + $this->phpDocParser = new PhpDocParser(new TypeParser($constExprParser), $constExprParser); + } + +} From 71c813d0fa1d29c2dfdef5a22f113d2459de13ef Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Sun, 7 Feb 2021 19:31:11 +0100 Subject: [PATCH 2/4] add editorconfig to keep tabs tabs --- .editorconfig | 9 +++++ src/Ast/BaseNode.php | 7 ++-- .../PHPStan/Ast/Attributes/AttributesTest.php | 38 +++++++++++++------ 3 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..410fc598 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +root = true + +[*.php] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true +indent_style = tab +indent_size = 4 diff --git a/src/Ast/BaseNode.php b/src/Ast/BaseNode.php index 05b540ea..e9c81282 100644 --- a/src/Ast/BaseNode.php +++ b/src/Ast/BaseNode.php @@ -8,9 +8,10 @@ abstract class BaseNode implements Node /** @var array */ private $attributes = []; - /** - * @param mixed $value - */ + /** + * @param string $key + * @param mixed $value + */ public function setAttribute(string $key, $value): void { $this->attributes[$key] = $value; diff --git a/tests/PHPStan/Ast/Attributes/AttributesTest.php b/tests/PHPStan/Ast/Attributes/AttributesTest.php index ff92ccd5..26dd3b11 100644 --- a/tests/PHPStan/Ast/Attributes/AttributesTest.php +++ b/tests/PHPStan/Ast/Attributes/AttributesTest.php @@ -2,31 +2,47 @@ namespace PHPStan\PhpDocParser\Ast\Attributes; +use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode; use PHPStan\PhpDocParser\Lexer\Lexer; use PHPStan\PhpDocParser\Parser\ConstExprParser; use PHPStan\PhpDocParser\Parser\PhpDocParser; +use PHPStan\PhpDocParser\Parser\TokenIterator; use PHPStan\PhpDocParser\Parser\TypeParser; use PHPUnit\Framework\TestCase; final class AttributesTest extends TestCase { - /** - * @var Lexer - */ - private $lexer; - - /** - * @var PhpDocParser - */ - private $phpDocParser; + /** @var PhpDocNode */ + private $phpDocNode; protected function setUp(): void { parent::setUp(); - $this->lexer = new Lexer(); + $lexer = new Lexer(); $constExprParser = new ConstExprParser(); - $this->phpDocParser = new PhpDocParser(new TypeParser($constExprParser), $constExprParser); + $phpDocParser = new PhpDocParser(new TypeParser($constExprParser), $constExprParser); + + $input = '/** @var string */'; + $tokens = new TokenIterator($lexer->tokenize($input)); + $this->phpDocNode = $phpDocParser->parse($tokens); + } + + public function testGetAttribute(): void + { + $defaultValue = $this->phpDocNode->getAttribute('unknown_with_default', 100); + $this->assertSame(100, $defaultValue); + + $unKnownValue = $this->phpDocNode->getAttribute('unknown'); + $this->assertNull($unKnownValue); + } + + public function testSetAttribute(): void + { + $this->phpDocNode->setAttribute('key', 'value'); + + $attributeValue = $this->phpDocNode->getAttribute('key'); + $this->assertSame('value', $attributeValue); } } From e31fc92af00ff97f683eae81b9a5ed5775da5ae5 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Fri, 12 Mar 2021 13:45:13 +0100 Subject: [PATCH 3/4] remove default --- src/Ast/BaseNode.php | 5 ++--- tests/PHPStan/Ast/Attributes/AttributesTest.php | 3 --- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Ast/BaseNode.php b/src/Ast/BaseNode.php index e9c81282..f14ca0b7 100644 --- a/src/Ast/BaseNode.php +++ b/src/Ast/BaseNode.php @@ -23,16 +23,15 @@ public function hasAttribute(string $key): bool } /** - * @param mixed|null $default * @return mixed|null */ - public function getAttribute(string $key, $default = null) + public function getAttribute(string $key) { if ($this->hasAttribute($key)) { return $this->attributes[$key]; } - return $default; + return null; } } diff --git a/tests/PHPStan/Ast/Attributes/AttributesTest.php b/tests/PHPStan/Ast/Attributes/AttributesTest.php index 26dd3b11..a20747ec 100644 --- a/tests/PHPStan/Ast/Attributes/AttributesTest.php +++ b/tests/PHPStan/Ast/Attributes/AttributesTest.php @@ -30,9 +30,6 @@ protected function setUp(): void public function testGetAttribute(): void { - $defaultValue = $this->phpDocNode->getAttribute('unknown_with_default', 100); - $this->assertSame(100, $defaultValue); - $unKnownValue = $this->phpDocNode->getAttribute('unknown'); $this->assertNull($unKnownValue); } From a0434777a7a25cd4ce58e72754c74a9d58c9d3aa Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Fri, 12 Mar 2021 13:48:58 +0100 Subject: [PATCH 4/4] [cs] use consistence-community, add explicit code_sniffer to known the version --- build-cs/composer.json | 3 ++- build.xml | 1 - phpcs.xml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/build-cs/composer.json b/build-cs/composer.json index 7a3d4e64..4e95b395 100644 --- a/build-cs/composer.json +++ b/build-cs/composer.json @@ -3,8 +3,9 @@ "php": "^7.1 || ^8.0" }, "require-dev": { - "consistence/coding-standard": "^3.10", + "consistence-community/coding-standard": "^3.10", "dealerdirect/phpcodesniffer-composer-installer": "^0.7.1", + "squizlabs/php_codesniffer": "^3.5", "slevomat/coding-standard": "^6.4.1" } } diff --git a/build.xml b/build.xml index 04716105..7d72b164 100644 --- a/build.xml +++ b/build.xml @@ -60,7 +60,6 @@ - - +