Skip to content

Commit

Permalink
fix phpstorm signature generator Pure attribute interpretation
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Aug 21, 2023
1 parent f9f4184 commit 0cf28a6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 153 deletions.
2 changes: 0 additions & 2 deletions bin/functionMetadata_original.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@
'chown' => ['hasSideEffects' => true],
'copy' => ['hasSideEffects' => true],
'count' => ['hasSideEffects' => false],
'connection_aborted' => ['hasSideEffects' => true],
'connection_status' => ['hasSideEffects' => true],
'fclose' => ['hasSideEffects' => true],
'fflush' => ['hasSideEffects' => true],
'fgetc' => ['hasSideEffects' => true],
Expand Down
20 changes: 18 additions & 2 deletions bin/generate-function-metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use JetBrains\PhpStorm\Pure;
use PhpParser\Node;
use PhpParser\Node\Attribute;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\NodeVisitor\NodeConnectingVisitor;
Expand Down Expand Up @@ -33,7 +34,7 @@ public function enterNode(Node $node)
if ($node instanceof Node\Stmt\Function_) {
foreach ($node->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attr) {
if ($attr->name->toString() === Pure::class) {
if ($this->isHasSideEffectsAnnotated($attr)) {
$this->functions[] = $node->namespacedName->toLowerString();
break 2;
}
Expand All @@ -49,7 +50,7 @@ public function enterNode(Node $node)
$className = $class->namespacedName->toString();
foreach ($node->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attr) {
if ($attr->name->toString() === Pure::class) {
if ($this->isHasSideEffectsAnnotated($attr)) {
$this->methods[] = sprintf('%s::%s', $className, $node->name->toString());
break 2;
}
Expand All @@ -60,6 +61,21 @@ public function enterNode(Node $node)
return null;
}

private function isHasSideEffectsAnnotated(Attribute $attr)
{
// Pure attribute by its own means that a function doesn't have side effects
if ($attr->name->toString() !== Pure::class) {
return false;
}

if ($attr->args === []) {
return true;
}

// The 1st parameter means that the result of function execution can change depending on the state of the "system"
return $attr->args[0]->value instanceof Node\Expr\ConstFetch && $attr->args[0]->value->name->toLowerString() !== 'true';
}

};

foreach ($finder as $stubFile) {
Expand Down
Loading

0 comments on commit 0cf28a6

Please sign in to comment.