Allow nodes to have custom attributes

This commit is contained in:
TomasVotruba 2021-02-07 19:08:48 +01:00 committed by Ondrej Mirtes
parent 37a74dfe11
commit cfe3c783bc
No known key found for this signature in database
GPG Key ID: 8E730BA25823D8B5
44 changed files with 243 additions and 0 deletions

View File

@ -2,9 +2,13 @@
namespace PHPStan\PhpDocParser\Ast\ConstExpr;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
class ConstExprArrayItemNode implements ConstExprNode
{
use NodeAttributes;
/** @var ConstExprNode|null */
public $key;

View File

@ -2,9 +2,13 @@
namespace PHPStan\PhpDocParser\Ast\ConstExpr;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
class ConstExprArrayNode implements ConstExprNode
{
use NodeAttributes;
/** @var ConstExprArrayItemNode[] */
public $items;

View File

@ -2,9 +2,13 @@
namespace PHPStan\PhpDocParser\Ast\ConstExpr;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
class ConstExprFalseNode implements ConstExprNode
{
use NodeAttributes;
public function __toString(): string
{
return 'false';

View File

@ -2,9 +2,13 @@
namespace PHPStan\PhpDocParser\Ast\ConstExpr;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
class ConstExprFloatNode implements ConstExprNode
{
use NodeAttributes;
/** @var string */
public $value;

View File

@ -2,9 +2,13 @@
namespace PHPStan\PhpDocParser\Ast\ConstExpr;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
class ConstExprIntegerNode implements ConstExprNode
{
use NodeAttributes;
/** @var string */
public $value;

View File

@ -2,9 +2,13 @@
namespace PHPStan\PhpDocParser\Ast\ConstExpr;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
class ConstExprNullNode implements ConstExprNode
{
use NodeAttributes;
public function __toString(): string
{
return 'null';

View File

@ -2,9 +2,13 @@
namespace PHPStan\PhpDocParser\Ast\ConstExpr;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
class ConstExprStringNode implements ConstExprNode
{
use NodeAttributes;
/** @var string */
public $value;

View File

@ -2,9 +2,13 @@
namespace PHPStan\PhpDocParser\Ast\ConstExpr;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
class ConstExprTrueNode implements ConstExprNode
{
use NodeAttributes;
public function __toString(): string
{
return 'true';

View File

@ -2,9 +2,13 @@
namespace PHPStan\PhpDocParser\Ast\ConstExpr;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
class ConstFetchNode implements ConstExprNode
{
use NodeAttributes;
/** @var string class name for class constants or empty string for non-class constants */
public $className;

View File

@ -7,4 +7,18 @@ interface Node
public function __toString(): string;
/**
* @param string $key
* @param mixed $value
*/
public function setAttribute(string $key, $value): void;
public function hasAttribute(string $key): bool;
/**
* @param string $key
* @return mixed
*/
public function getAttribute(string $key);
}

View File

@ -0,0 +1,38 @@
<?php declare(strict_types = 1);
namespace PHPStan\PhpDocParser\Ast;
trait NodeAttributes
{
/** @var array<string, mixed> */
private $attributes = [];
/**
* @param string $key
* @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 string $key
* @return mixed
*/
public function getAttribute(string $key)
{
if ($this->hasAttribute($key)) {
return $this->attributes[$key];
}
return null;
}
}

View File

@ -2,9 +2,13 @@
namespace PHPStan\PhpDocParser\Ast\PhpDoc;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
class DeprecatedTagValueNode implements PhpDocTagValueNode
{
use NodeAttributes;
/** @var string (may be empty) */
public $description;

View File

@ -2,11 +2,14 @@
namespace PHPStan\PhpDocParser\Ast\PhpDoc;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
class ExtendsTagValueNode implements PhpDocTagValueNode
{
use NodeAttributes;
/** @var GenericTypeNode */
public $type;

View File

@ -2,9 +2,13 @@
namespace PHPStan\PhpDocParser\Ast\PhpDoc;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
class GenericTagValueNode implements PhpDocTagValueNode
{
use NodeAttributes;
/** @var string (may be empty) */
public $value;

View File

@ -2,11 +2,14 @@
namespace PHPStan\PhpDocParser\Ast\PhpDoc;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
class ImplementsTagValueNode implements PhpDocTagValueNode
{
use NodeAttributes;
/** @var GenericTypeNode */
public $type;

View File

@ -2,9 +2,13 @@
namespace PHPStan\PhpDocParser\Ast\PhpDoc;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
class InvalidTagValueNode implements PhpDocTagValueNode
{
use NodeAttributes;
/** @var string (may be empty) */
public $value;

View File

@ -2,11 +2,14 @@
namespace PHPStan\PhpDocParser\Ast\PhpDoc;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
class MethodTagValueNode implements PhpDocTagValueNode
{
use NodeAttributes;
/** @var bool */
public $isStatic;

View File

@ -4,11 +4,14 @@ namespace PHPStan\PhpDocParser\Ast\PhpDoc;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprNode;
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
class MethodTagValueParameterNode implements Node
{
use NodeAttributes;
/** @var TypeNode|null */
public $type;

View File

@ -2,11 +2,14 @@
namespace PHPStan\PhpDocParser\Ast\PhpDoc;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
class MixinTagValueNode implements PhpDocTagValueNode
{
use NodeAttributes;
/** @var TypeNode */
public $type;

View File

@ -2,11 +2,14 @@
namespace PHPStan\PhpDocParser\Ast\PhpDoc;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
class ParamTagValueNode implements PhpDocTagValueNode
{
use NodeAttributes;
/** @var TypeNode */
public $type;

View File

@ -3,10 +3,13 @@
namespace PHPStan\PhpDocParser\Ast\PhpDoc;
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
class PhpDocNode implements Node
{
use NodeAttributes;
/** @var PhpDocChildNode[] */
public $children;

View File

@ -2,9 +2,13 @@
namespace PHPStan\PhpDocParser\Ast\PhpDoc;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
class PhpDocTagNode implements PhpDocChildNode
{
use NodeAttributes;
/** @var string */
public $name;

View File

@ -2,9 +2,13 @@
namespace PHPStan\PhpDocParser\Ast\PhpDoc;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
class PhpDocTextNode implements PhpDocChildNode
{
use NodeAttributes;
/** @var string */
public $text;

View File

@ -2,11 +2,14 @@
namespace PHPStan\PhpDocParser\Ast\PhpDoc;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
class PropertyTagValueNode implements PhpDocTagValueNode
{
use NodeAttributes;
/** @var TypeNode */
public $type;

View File

@ -2,11 +2,14 @@
namespace PHPStan\PhpDocParser\Ast\PhpDoc;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
class ReturnTagValueNode implements PhpDocTagValueNode
{
use NodeAttributes;
/** @var TypeNode */
public $type;

View File

@ -2,11 +2,14 @@
namespace PHPStan\PhpDocParser\Ast\PhpDoc;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
class TemplateTagValueNode implements PhpDocTagValueNode
{
use NodeAttributes;
/** @var string */
public $name;

View File

@ -2,11 +2,14 @@
namespace PHPStan\PhpDocParser\Ast\PhpDoc;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
class ThrowsTagValueNode implements PhpDocTagValueNode
{
use NodeAttributes;
/** @var TypeNode */
public $type;

View File

@ -2,11 +2,14 @@
namespace PHPStan\PhpDocParser\Ast\PhpDoc;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
class TypeAliasImportTagValueNode implements PhpDocTagValueNode
{
use NodeAttributes;
/** @var string */
public $importedAlias;

View File

@ -2,11 +2,14 @@
namespace PHPStan\PhpDocParser\Ast\PhpDoc;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
class TypeAliasTagValueNode implements PhpDocTagValueNode
{
use NodeAttributes;
/** @var string */
public $alias;

View File

@ -2,11 +2,14 @@
namespace PHPStan\PhpDocParser\Ast\PhpDoc;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
class UsesTagValueNode implements PhpDocTagValueNode
{
use NodeAttributes;
/** @var GenericTypeNode */
public $type;

View File

@ -2,11 +2,14 @@
namespace PHPStan\PhpDocParser\Ast\PhpDoc;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
class VarTagValueNode implements PhpDocTagValueNode
{
use NodeAttributes;
/** @var TypeNode */
public $type;

View File

@ -4,10 +4,13 @@ namespace PHPStan\PhpDocParser\Ast\Type;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprStringNode;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
class ArrayShapeItemNode implements TypeNode
{
use NodeAttributes;
/** @var ConstExprIntegerNode|ConstExprStringNode|IdentifierTypeNode|null */
public $keyName;

View File

@ -2,9 +2,13 @@
namespace PHPStan\PhpDocParser\Ast\Type;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
class ArrayShapeNode implements TypeNode
{
use NodeAttributes;
/** @var ArrayShapeItemNode[] */
public $items;

View File

@ -2,9 +2,13 @@
namespace PHPStan\PhpDocParser\Ast\Type;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
class ArrayTypeNode implements TypeNode
{
use NodeAttributes;
/** @var TypeNode */
public $type;

View File

@ -2,9 +2,13 @@
namespace PHPStan\PhpDocParser\Ast\Type;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
class CallableTypeNode implements TypeNode
{
use NodeAttributes;
/** @var IdentifierTypeNode */
public $identifier;

View File

@ -3,10 +3,13 @@
namespace PHPStan\PhpDocParser\Ast\Type;
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
class CallableTypeParameterNode implements Node
{
use NodeAttributes;
/** @var TypeNode */
public $type;

View File

@ -3,10 +3,13 @@
namespace PHPStan\PhpDocParser\Ast\Type;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprNode;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
class ConstTypeNode implements TypeNode
{
use NodeAttributes;
/** @var ConstExprNode */
public $constExpr;

View File

@ -2,9 +2,13 @@
namespace PHPStan\PhpDocParser\Ast\Type;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
class GenericTypeNode implements TypeNode
{
use NodeAttributes;
/** @var IdentifierTypeNode */
public $type;

View File

@ -2,9 +2,13 @@
namespace PHPStan\PhpDocParser\Ast\Type;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
class IdentifierTypeNode implements TypeNode
{
use NodeAttributes;
/** @var string */
public $name;

View File

@ -2,9 +2,13 @@
namespace PHPStan\PhpDocParser\Ast\Type;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
class IntersectionTypeNode implements TypeNode
{
use NodeAttributes;
/** @var TypeNode[] */
public $types;

View File

@ -2,9 +2,13 @@
namespace PHPStan\PhpDocParser\Ast\Type;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
class NullableTypeNode implements TypeNode
{
use NodeAttributes;
/** @var TypeNode */
public $type;

View File

@ -2,9 +2,13 @@
namespace PHPStan\PhpDocParser\Ast\Type;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
class ThisTypeNode implements TypeNode
{
use NodeAttributes;
public function __toString(): string
{
return '$this';

View File

@ -2,9 +2,13 @@
namespace PHPStan\PhpDocParser\Ast\Type;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
class UnionTypeNode implements TypeNode
{
use NodeAttributes;
/** @var TypeNode[] */
public $types;

View File

@ -0,0 +1,45 @@
<?php declare(strict_types = 1);
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 PhpDocNode */
private $phpDocNode;
protected function setUp(): void
{
parent::setUp();
$lexer = new Lexer();
$constExprParser = new 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
{
$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);
}
}