Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions src/NodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
);

Expand All @@ -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);
}
}
}

Expand All @@ -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;
}
}
Expand Down
10 changes: 10 additions & 0 deletions test/files/constants.in.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
8 changes: 8 additions & 0 deletions test/files/constants.out.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@
\define('FIZ', 'BUZ');

const A = 1, B = 2;

\define('ACME', 1);

\define(
'FOOBAR',
/** @var int */
0
);
8 changes: 6 additions & 2 deletions test/files/globals.all.out.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<?php
/** doc */
/** @var string */
$a = 'a' . 'a';

/** @doc */
/** @var string $b */
$b = 'b' . 'b';

$c = 'c' . 'c';

$d = 'd' . 'd';

$x = 'x' . 'x';

$y = 'y' . 'y';
4 changes: 2 additions & 2 deletions test/files/globals.doc.out.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/** doc */
/** @var string */
$a = 'a' . 'a';

/** @doc */
/** @var string $b */
$b = 'b' . 'b';
10 changes: 8 additions & 2 deletions test/files/globals.in.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
/** doc */
/** @var string */
$a = 'a' . 'a';

/** @doc */
/** @var string $b */
$GLOBALS['b'] = 'b' . 'b';

$c = 'c' . 'c';
Expand All @@ -25,3 +25,9 @@

function () {
};

/** doc */
$x = 'x' . 'x';

/** @doc */
$GLOBALS['y'] = 'y' . 'y';
4 changes: 4 additions & 0 deletions test/files/globals.no-doc.out.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
$c = 'c' . 'c';

$d = 'd' . 'd';

$x = 'x' . 'x';

$y = 'y' . 'y';
8 changes: 6 additions & 2 deletions test/files/globals.nullified.out.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<?php
/** doc */
/** @var string */
$a = \null;

/** @doc */
/** @var string $b */
$b = \null;

$c = \null;

$d = \null;

$x = \null;

$y = \null;