Skip to content

Commit

Permalink
functionMetadata - read #[Pure] from methods in phpstorm-stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 1, 2020
1 parent c8e4f8b commit 500b160
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 7 deletions.
42 changes: 35 additions & 7 deletions bin/generate-function-metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\NodeVisitor\NodeConnectingVisitor;
use PhpParser\ParserFactory;

(function () {
Expand All @@ -18,17 +19,34 @@
/** @var string[] */
public $functions = [];

/** @var string[] */
public $methods = [];

public function enterNode(Node $node)
{
if (!$node instanceof Node\Stmt\Function_) {
return;
if ($node instanceof Node\Stmt\Function_) {
foreach ($node->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attr) {
if ($attr->name->toString() === \JetBrains\PhpStorm\Pure::class) {
$this->functions[] = $node->namespacedName->toLowerString();
break;
}
}
}
}

foreach ($node->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attr) {
if ($attr->name->toString() === \JetBrains\PhpStorm\Pure::class) {
$this->functions[] = $node->namespacedName->toLowerString();
break;
if ($node instanceof Node\Stmt\ClassMethod) {
$class = $node->getAttribute('parent');
if (!$class instanceof Node\Stmt\ClassLike) {
throw new \PHPStan\ShouldNotHappenException($node->name->toString());
}
$className = $class->namespacedName->toString();
foreach ($node->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attr) {
if ($attr->name->toString() === \JetBrains\PhpStorm\Pure::class) {
$this->methods[] = sprintf('%s::%s', $className, $node->name->toString());
break;
}
}
}
}
Expand All @@ -41,6 +59,7 @@ public function enterNode(Node $node)
$path = $stubFile->getPathname();
$traverser = new NodeTraverser();
$traverser->addVisitor(new NameResolver());
$traverser->addVisitor(new NodeConnectingVisitor());
$traverser->addVisitor($visitor);

$traverser->traverse(
Expand All @@ -58,6 +77,15 @@ public function enterNode(Node $node)
$metadata[$functionName] = ['hasSideEffects' => false];
}

foreach ($visitor->methods as $methodName) {
if (array_key_exists($methodName, $metadata)) {
if ($metadata[$methodName]['hasSideEffects']) {
throw new \PHPStan\ShouldNotHappenException($methodName);
}
}
$metadata[$methodName] = ['hasSideEffects' => false];
}

ksort($metadata);

$template = <<<'php'
Expand Down
36 changes: 36 additions & 0 deletions resources/functionMetadata.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
<?php declare(strict_types = 1);

return [
'Cassandra\\Exception\\AlreadyExistsException::__construct' => ['hasSideEffects' => false],
'Cassandra\\Exception\\AuthenticationException::__construct' => ['hasSideEffects' => false],
'Cassandra\\Exception\\ConfigurationException::__construct' => ['hasSideEffects' => false],
'Cassandra\\Exception\\DivideByZeroException::__construct' => ['hasSideEffects' => false],
'Cassandra\\Exception\\DomainException::__construct' => ['hasSideEffects' => false],
'Cassandra\\Exception\\ExecutionException::__construct' => ['hasSideEffects' => false],
'Cassandra\\Exception\\InvalidArgumentException::__construct' => ['hasSideEffects' => false],
'Cassandra\\Exception\\InvalidQueryException::__construct' => ['hasSideEffects' => false],
'Cassandra\\Exception\\InvalidSyntaxException::__construct' => ['hasSideEffects' => false],
'Cassandra\\Exception\\IsBootstrappingException::__construct' => ['hasSideEffects' => false],
'Cassandra\\Exception\\LogicException::__construct' => ['hasSideEffects' => false],
'Cassandra\\Exception\\OverloadedException::__construct' => ['hasSideEffects' => false],
'Cassandra\\Exception\\ProtocolException::__construct' => ['hasSideEffects' => false],
'Cassandra\\Exception\\RangeException::__construct' => ['hasSideEffects' => false],
'Cassandra\\Exception\\ReadTimeoutException::__construct' => ['hasSideEffects' => false],
'Cassandra\\Exception\\RuntimeException::__construct' => ['hasSideEffects' => false],
'Cassandra\\Exception\\ServerException::__construct' => ['hasSideEffects' => false],
'Cassandra\\Exception\\TimeoutException::__construct' => ['hasSideEffects' => false],
'Cassandra\\Exception\\TruncateException::__construct' => ['hasSideEffects' => false],
'Cassandra\\Exception\\UnauthorizedException::__construct' => ['hasSideEffects' => false],
'Cassandra\\Exception\\UnavailableException::__construct' => ['hasSideEffects' => false],
'Cassandra\\Exception\\UnpreparedException::__construct' => ['hasSideEffects' => false],
'Cassandra\\Exception\\ValidationException::__construct' => ['hasSideEffects' => false],
'Cassandra\\Exception\\WriteTimeoutException::__construct' => ['hasSideEffects' => false],
'DateTime::add' => ['hasSideEffects' => true],
'DateTime::createFromFormat' => ['hasSideEffects' => false],
'DateTime::createFromImmutable' => ['hasSideEffects' => false],
Expand Down Expand Up @@ -33,6 +57,18 @@
'DateTimeImmutable::setTimestamp' => ['hasSideEffects' => false],
'DateTimeImmutable::setTimezone' => ['hasSideEffects' => false],
'DateTimeImmutable::sub' => ['hasSideEffects' => false],
'ErrorException::__construct' => ['hasSideEffects' => false],
'Exception::__construct' => ['hasSideEffects' => false],
'Exception::getCode' => ['hasSideEffects' => false],
'Exception::getFile' => ['hasSideEffects' => false],
'Exception::getLine' => ['hasSideEffects' => false],
'Exception::getMessage' => ['hasSideEffects' => false],
'Exception::getPrevious' => ['hasSideEffects' => false],
'Exception::getTrace' => ['hasSideEffects' => false],
'Exception::getTraceAsString' => ['hasSideEffects' => false],
'MemcachedException::__construct' => ['hasSideEffects' => false],
'SQLiteException::__construct' => ['hasSideEffects' => false],
'SoapFault::__construct' => ['hasSideEffects' => false],
'_' => ['hasSideEffects' => false],
'abs' => ['hasSideEffects' => false],
'acos' => ['hasSideEffects' => false],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public function testRule(): void
'Call to static method DateTimeImmutable::createFromFormat() on a separate line has no effect.',
16,
],
[
'Call to method Exception::getCode() on a separate line has no effect.',
21,
],
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ public function doBar(\DateTimeImmutable $dti)
$dti->createFromFormat('Y-m-d', '2019-07-24');
}

public function doBaz(\Exception $e)
{
$e->getCode();
}

}

0 comments on commit 500b160

Please sign in to comment.