diff --git a/src/NodeVisitor.php b/src/NodeVisitor.php index 0e631dc..27207c0 100644 --- a/src/NodeVisitor.php +++ b/src/NodeVisitor.php @@ -376,8 +376,14 @@ private function needsNode(Node $node, string $namespace): bool $node->consts, function (\PhpParser\Node\Const_ $const) { $fullyQualifiedName = $const->name->name; - return $this->count('constants', $fullyQualifiedName) - && !defined($fullyQualifiedName); + + if ( + !$this->isInIf || + !isset($this->counts['constants'][$fullyQualifiedName]) + ) { + return $this->count('constants', $fullyQualifiedName) + && !defined($fullyQualifiedName); + } } ); @@ -392,8 +398,13 @@ function (\PhpParser\Node\Const_ $const) { ) { $fullyQualifiedName = $node->expr->args[0]->value->value; - return $this->count('constants', $fullyQualifiedName) - && !defined($fullyQualifiedName); + if ( + !$this->isInIf || + !isset($this->counts['constants'][$fullyQualifiedName]) + ) { + return $this->count('constants', $fullyQualifiedName) + && !defined($fullyQualifiedName); + } } } @@ -404,12 +415,24 @@ function (\PhpParser\Node\Const_ $const) { && $node->expr->var instanceof Variable && is_string($node->expr->var->name) ) { - $this->count('globals', $node->expr->var->name); // We'll keep regular global variable declarations, depending on // whether or not they are documented. - if ($node->getDocComment()) { + // Only @var comments are actual, useful documentation for stubs + $hasValidComment = $node->getDocComment() && preg_match('/@(?:[a-z]+-)?var\s/', $node->getDocComment()->getText()); + + if ($this->needsDocumentedGlobals && $hasValidComment) { + $this->count('globals', $node->expr->var->name); return $this->needsDocumentedGlobals; - } else { + } + + if ($this->needsUndocumentedGlobals && !$hasValidComment) { + $this->count('globals', $node->expr->var->name); + + // remove useless comments + if ($node->getComments() !== []) { + $node->setAttribute('comments', []); + } + return $this->needsUndocumentedGlobals; } } diff --git a/test/files/constants.in.php b/test/files/constants.in.php index b9e2471..c0e71f1 100644 --- a/test/files/constants.in.php +++ b/test/files/constants.in.php @@ -7,3 +7,13 @@ define('FIZ', 'BUZ'); const A = 1, B = 2; + +if (!defined('ACME')) { + define('ACME', 1); +} + +if (!defined('ACME')) { + define('ACME', 2); +} + +\define( 'FOOBAR', /** @var int */ 0 ); diff --git a/test/files/constants.out.php b/test/files/constants.out.php index 8db4891..6c3e9ce 100644 --- a/test/files/constants.out.php +++ b/test/files/constants.out.php @@ -7,3 +7,11 @@ \define('FIZ', 'BUZ'); const A = 1, B = 2; + +\define('ACME', 1); + +\define( + 'FOOBAR', + /** @var int */ + 0 +); diff --git a/test/files/globals.all.out.php b/test/files/globals.all.out.php index 710d9fc..3ed76ab 100644 --- a/test/files/globals.all.out.php +++ b/test/files/globals.all.out.php @@ -1,10 +1,14 @@