Skip to content

Commit

Permalink
Add support for attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
olsavmic committed Jun 15, 2022
1 parent 6568103 commit 865dc9f
Show file tree
Hide file tree
Showing 9 changed files with 410 additions and 43 deletions.
76 changes: 76 additions & 0 deletions src/Dependency/ExportedNode/ExportedAttributeArgumentNode.php
@@ -0,0 +1,76 @@
<?php declare(strict_types = 1);

namespace PHPStan\Dependency\ExportedNode;

use JsonSerializable;
use PHPStan\Dependency\ExportedNode;

class ExportedAttributeArgumentNode implements ExportedNode, JsonSerializable
{

private ?string $name;

private string $value;

private bool $byRef;

public function __construct(?string $name, string $value, bool $byRef)
{
$this->name = $name;
$this->value = $value;
$this->byRef = $byRef;
}

public function equals(ExportedNode $node): bool
{
if (!$node instanceof self) {
return false;
}

return $this->name === $node->name
&& $this->value === $node->value
&& $this->byRef === $node->byRef;
}

/**
* @param mixed[] $properties
* @return self
*/
public static function __set_state(array $properties): ExportedNode
{
return new self(
$properties['name'],
$properties['value'],
$properties['byRef'],
);
}

/**
* @return mixed
*/
public function jsonSerialize()
{
return [
'type' => self::class,
'data' => [
'name' => $this->name,
'value' => $this->value,
'byRef' => $this->byRef,
],
];
}

/**
* @param mixed[] $data
* @return self
*/
public static function decode(array $data): ExportedNode
{
return new self(
$data['name'],
$data['value'],
$data['byRef'],
);
}

}
92 changes: 92 additions & 0 deletions src/Dependency/ExportedNode/ExportedAttributeNode.php
@@ -0,0 +1,92 @@
<?php declare(strict_types = 1);

namespace PHPStan\Dependency\ExportedNode;

use JsonSerializable;
use PHPStan\Dependency\ExportedNode;

class ExportedAttributeNode implements ExportedNode, JsonSerializable
{

private string $name;

/** @var ExportedAttributeArgumentNode[] $args */
private array $args;

/**
* @param ExportedAttributeArgumentNode[] $args
*/
public function __construct(
string $name,
array $args
)
{
$this->name = $name;
$this->args = $args;
}

public function equals(ExportedNode $node): bool
{
if (!$node instanceof self) {
return false;
}

if (count($this->args) !== count($node->args)) {
return false;
}

foreach ($this->args as $i => $ourAttribute) {
$theirAttribute = $node->args[$i];
if (!$ourAttribute->equals($theirAttribute)) {
return false;
}
}


return $this->name === $node->name;
}

/**
* @param mixed[] $properties
* @return self
*/
public static function __set_state(array $properties): ExportedNode
{
return new self(
$properties['name'],
$properties['args'],
);
}

/**
* @return mixed
*/
public function jsonSerialize()
{
return [
'type' => self::class,
'data' => [
'name' => $this->name,
'args' => $this->args,
],
];
}

/**
* @param mixed[] $data
* @return self
*/
public static function decode(array $data): ExportedNode
{
return new self(
$data['name'],
array_map(static function (array $parameterData): ExportedAttributeArgumentNode {
if ($parameterData['type'] !== ExportedAttributeArgumentNode::class) {
throw new \PHPStan\ShouldNotHappenException();
}
return ExportedAttributeArgumentNode::decode($parameterData['data']);
}, $data['args'])
);
}

}
46 changes: 40 additions & 6 deletions src/Dependency/ExportedNode/ExportedClassConstantNode.php
Expand Up @@ -20,15 +20,30 @@ class ExportedClassConstantNode implements ExportedNode, JsonSerializable

private ?ExportedPhpDocNode $phpDoc;

public function __construct(string $name, string $value, bool $public, bool $private, bool $final, ?ExportedPhpDocNode $phpDoc)
/** @var ExportedAttributeNode[] */
private array $attributes;

/**
* @param ExportedAttributeNode[] $attributes
*/
public function __construct(
string $name,
string $value,
bool $public,
bool $private,
bool $final,
?ExportedPhpDocNode $phpDoc,
array $attributes
)
{
$this->name = $name;
$this->value = $value;
$this->public = $public;
$this->private = $private;
$this->final = $final;
$this->phpDoc = $phpDoc;
}
$this->attributes = $attributes;
}

public function equals(ExportedNode $node): bool
{
Expand All @@ -48,6 +63,17 @@ public function equals(ExportedNode $node): bool
return false;
}

if (count($this->attributes) !== count($node->attributes)) {
return false;
}

foreach ($this->attributes as $i => $ourAttribute) {
$theirAttribute = $node->attributes[$i];
if (!$ourAttribute->equals($theirAttribute)) {
return false;
}
}

return $this->name === $node->name
&& $this->value === $node->value
&& $this->public === $node->public
Expand All @@ -67,8 +93,9 @@ public static function __set_state(array $properties): ExportedNode
$properties['public'],
$properties['private'],
$properties['final'],
$properties['phpDoc']
);
$properties['phpDoc'],
$properties['attributes']
);
}

/**
Expand All @@ -83,7 +110,13 @@ public static function decode(array $data): ExportedNode
$data['public'],
$data['private'],
$data['final'],
$data['phpDoc'] !== null ? ExportedPhpDocNode::decode($data['phpDoc']['data']) : null
$data['phpDoc'] !== null ? ExportedPhpDocNode::decode($data['phpDoc']['data']) : null,
array_map(static function (array $parameterData): ExportedAttributeNode {
if ($parameterData['type'] !== ExportedAttributeNode::class) {
throw new \PHPStan\ShouldNotHappenException();
}
return ExportedAttributeNode::decode($parameterData['data']);
}, $data['attributes'])
);
}

Expand All @@ -101,7 +134,8 @@ public function jsonSerialize()
'private' => $this->private,
'final' => $this->final,
'phpDoc' => $this->phpDoc,
],
'attributes' => $this->attributes,
],
];
}

Expand Down
37 changes: 31 additions & 6 deletions src/Dependency/ExportedNode/ExportedClassNode.php
Expand Up @@ -27,6 +27,9 @@ class ExportedClassNode implements ExportedNode, JsonSerializable
/** @var ExportedTraitUseAdaptation[] */
private array $traitUseAdaptations;

/** @var ExportedAttributeNode[] */
private array $attributes;

/**
* @param string $name
* @param ExportedPhpDocNode|null $phpDoc
Expand All @@ -36,6 +39,7 @@ class ExportedClassNode implements ExportedNode, JsonSerializable
* @param string[] $implements
* @param string[] $usedTraits
* @param ExportedTraitUseAdaptation[] $traitUseAdaptations
* @param ExportedAttributeNode[] $attributes
*/
public function __construct(
string $name,
Expand All @@ -45,7 +49,8 @@ public function __construct(
?string $extends,
array $implements,
array $usedTraits,
array $traitUseAdaptations
array $traitUseAdaptations,
array $attributes
)
{
$this->name = $name;
Expand All @@ -56,7 +61,8 @@ public function __construct(
$this->implements = $implements;
$this->usedTraits = $usedTraits;
$this->traitUseAdaptations = $traitUseAdaptations;
}
$this->attributes = $attributes;
}

public function equals(ExportedNode $node): bool
{
Expand All @@ -76,6 +82,17 @@ public function equals(ExportedNode $node): bool
return false;
}

if (count($this->attributes) !== count($node->attributes)) {
return false;
}

foreach ($this->attributes as $i => $ourAttribute) {
$theirAttribute = $node->attributes[$i];
if (!$ourAttribute->equals($theirAttribute)) {
return false;
}
}

if (count($this->traitUseAdaptations) !== count($node->traitUseAdaptations)) {
return false;
}
Expand Down Expand Up @@ -109,8 +126,9 @@ public static function __set_state(array $properties): ExportedNode
$properties['extends'],
$properties['implements'],
$properties['usedTraits'],
$properties['traitUseAdaptations']
);
$properties['traitUseAdaptations'],
$properties['attributes']
);
}

/**
Expand All @@ -129,7 +147,8 @@ public function jsonSerialize()
'implements' => $this->implements,
'usedTraits' => $this->usedTraits,
'traitUseAdaptations' => $this->traitUseAdaptations,
],
'attributes' => $this->attributes,
],
];
}

Expand All @@ -152,7 +171,13 @@ public static function decode(array $data): ExportedNode
throw new \PHPStan\ShouldNotHappenException();
}
return ExportedTraitUseAdaptation::decode($traitUseAdaptationData['data']);
}, $data['traitUseAdaptations'])
}, $data['traitUseAdaptations']),
array_map(static function (array $parameterData): ExportedAttributeNode {
if ($parameterData['type'] !== ExportedAttributeNode::class) {
throw new \PHPStan\ShouldNotHappenException();
}
return ExportedAttributeNode::decode($parameterData['data']);
}, $data['attributes'])
);
}

Expand Down

0 comments on commit 865dc9f

Please sign in to comment.