New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ExportedNodeResolver - Add support for attributes #1427
Merged
ondrejmirtes
merged 2 commits into
phpstan:1.7.x
from
olsavmic:olsavmic-exported-node-resolver-support-attributes
Jun 17, 2022
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
src/Dependency/ExportedNode/ExportedAttributeArgumentNode.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Dependency\ExportedNode; | ||
|
||
use JsonSerializable; | ||
use PHPStan\Dependency\ExportedNode; | ||
|
||
class ExportedAttributeArgumentNode implements ExportedNode, JsonSerializable | ||
{ | ||
|
||
public function __construct( | ||
private ?string $name, | ||
private string $value, | ||
private bool $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'], | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Dependency\ExportedNode; | ||
|
||
use JsonSerializable; | ||
use PHPStan\Dependency\ExportedNode; | ||
use PHPStan\ShouldNotHappenException; | ||
use function array_map; | ||
use function count; | ||
|
||
class ExportedAttributeNode implements ExportedNode, JsonSerializable | ||
{ | ||
|
||
/** | ||
* @param ExportedAttributeArgumentNode[] $args | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
*/ | ||
public function __construct( | ||
private string $name, | ||
private array $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; | ||
olsavmic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* @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 ShouldNotHappenException(); | ||
} | ||
return ExportedAttributeArgumentNode::decode($parameterData['data']); | ||
}, $data['args']), | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: Most likely not even possible, test it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need the name here, being indexed correctly by
int|string
in ExportedAttributeNode should be fine.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so, you can switch named argument and keep the value & index.Ok :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the src/Dependency/ExportedNode/ExportedAttributeArgumentNode.php entirely as it was storing only the expression value. Let me know if you prefer to keep it