Skip to content

Commit

Permalink
Use getParts() instead of $parts on PhpParser\Node\Name.
Browse files Browse the repository at this point in the history
also use getFirst() and getString()
  • Loading branch information
ygottschalk committed Jun 28, 2023
1 parent 086b943 commit 0981a15
Show file tree
Hide file tree
Showing 36 changed files with 174 additions and 184 deletions.
2 changes: 1 addition & 1 deletion examples/plugins/FunctionCasingChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static function afterFunctionCallAnalysis(AfterFunctionCallAnalysisEvent

$function_name_parts = explode('\\', $function_storage->cased_name);

if (end($function_name_parts) !== end($expr->name->parts)) {
if (end($function_name_parts) !== $expr->name->getLast()) {
IssueBuffer::maybeAdd(
new IncorrectFunctionCasing(
'Function is incorrectly cased, expecting ' . $function_storage->cased_name,
Expand Down
2 changes: 1 addition & 1 deletion examples/plugins/StringChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static function afterExpressionAnalysis(AfterExpressionAnalysisEvent $eve
&& $expr->left->class instanceof PhpParser\Node\Name
&& $expr->left->name instanceof PhpParser\Node\Identifier
&& strtolower($expr->left->name->name) === 'class'
&& !in_array(strtolower($expr->left->class->parts[0]), ['self', 'static', 'parent'])
&& !in_array(strtolower($expr->left->class->getFirst()), ['self', 'static', 'parent'])
&& $expr->right instanceof PhpParser\Node\Scalar\String_
&& preg_match('/^::[A-Za-z0-9]+$/', $expr->right->value)
) {
Expand Down
7 changes: 3 additions & 4 deletions src/Psalm/Internal/Analyzer/CanAlias.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Psalm\FileManipulation;
use Psalm\Internal\FileManipulation\FileManipulationBuffer;

use function implode;
use function strtolower;

/**
Expand Down Expand Up @@ -51,7 +50,7 @@ public function visitUse(PhpParser\Node\Stmt\Use_ $stmt): void
$codebase = $this->getCodebase();

foreach ($stmt->uses as $use) {
$use_path = implode('\\', $use->name->parts);
$use_path = $use->name->toString();
$use_path_lc = strtolower($use_path);
$use_alias = $use->alias->name ?? $use->name->getLast();
$use_alias_lc = strtolower($use_alias);
Expand Down Expand Up @@ -106,12 +105,12 @@ public function visitUse(PhpParser\Node\Stmt\Use_ $stmt): void

public function visitGroupUse(PhpParser\Node\Stmt\GroupUse $stmt): void
{
$use_prefix = implode('\\', $stmt->prefix->parts);
$use_prefix = $stmt->prefix->toString();

$codebase = $this->getCodebase();

foreach ($stmt->uses as $use) {
$use_path = $use_prefix . '\\' . implode('\\', $use->name->parts);
$use_path = $use_prefix . '\\' . $use->name->toString();
$use_alias = $use->alias->name ?? $use->name->getLast();

switch ($use->type !== PhpParser\Node\Stmt\Use_::TYPE_UNKNOWN ? $use->type : $stmt->type) {
Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/Internal/Analyzer/ClassAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2021,7 +2021,7 @@ private function checkImplementedInterfaces(
. ($interface_name instanceof PhpParser\Node\Name\FullyQualified
? '\\'
: $this->getNamespace() . '-')
. implode('\\', $interface_name->parts),
. $interface_name->toString(),
);

$interface_location = new CodeLocation($this, $interface_name);
Expand Down Expand Up @@ -2437,7 +2437,7 @@ private function checkParentClass(
. ($extended_class instanceof PhpParser\Node\Name\FullyQualified
? '\\'
: $this->getNamespace() . '-')
. implode('\\', $extended_class->parts),
. $extended_class->toString(),
);
}

Expand Down
9 changes: 4 additions & 5 deletions src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
use function count;
use function explode;
use function gettype;
use function implode;
use function in_array;
use function preg_match;
use function preg_replace;
Expand Down Expand Up @@ -421,15 +420,15 @@ public static function getFQCLNFromNameObject(
}

if ($class_name instanceof PhpParser\Node\Name\FullyQualified) {
return implode('\\', $class_name->parts);
return $class_name->toString();
}

if (in_array($class_name->parts[0], ['self', 'static', 'parent'], true)) {
return $class_name->parts[0];
if (in_array($class_name->getFirst(), ['self', 'static', 'parent'], true)) {
return $class_name->getFirst();
}

return Type::getFQCLNFromString(
implode('\\', $class_name->parts),
$class_name->toString(),
$aliases,
);
}
Expand Down
3 changes: 1 addition & 2 deletions src/Psalm/Internal/Analyzer/FileAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
use function array_diff_key;
use function array_keys;
use function count;
use function implode;
use function strpos;
use function strtolower;

Expand Down Expand Up @@ -283,7 +282,7 @@ public function populateCheckers(array $stmts): array
} elseif ($stmt instanceof PhpParser\Node\Stmt\ClassLike) {
$this->populateClassLikeAnalyzers($stmt);
} elseif ($stmt instanceof PhpParser\Node\Stmt\Namespace_) {
$namespace_name = $stmt->name ? implode('\\', $stmt->name->parts) : '';
$namespace_name = $stmt->name ? $stmt->name->toString() : '';

$namespace_analyzer = new NamespaceAnalyzer($stmt, $this);
$namespace_analyzer->collectAnalyzableInformation();
Expand Down
3 changes: 1 addition & 2 deletions src/Psalm/Internal/Analyzer/NamespaceAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

use function assert;
use function count;
use function implode;
use function is_string;
use function preg_replace;
use function strpos;
Expand Down Expand Up @@ -49,7 +48,7 @@ public function __construct(Namespace_ $namespace, FileAnalyzer $source)
{
$this->source = $source;
$this->namespace = $namespace;
$this->namespace_name = $this->namespace->name ? implode('\\', $this->namespace->name->parts) : '';
$this->namespace_name = $this->namespace->name ? $this->namespace->name->toString() : '';
}

public function collectAnalyzableInformation(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,13 @@ private static function getDefinitelyEvaluatedExpressionAfterIf(PhpParser\Node\E
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\Identical
) {
if ($stmt->left instanceof PhpParser\Node\Expr\ConstFetch
&& $stmt->left->name->parts === ['true']
&& $stmt->left->name->getParts() === ['true']
) {
return self::getDefinitelyEvaluatedExpressionAfterIf($stmt->right);
}

if ($stmt->right instanceof PhpParser\Node\Expr\ConstFetch
&& $stmt->right->name->parts === ['true']
&& $stmt->right->name->getParts() === ['true']
) {
return self::getDefinitelyEvaluatedExpressionAfterIf($stmt->left);
}
Expand Down Expand Up @@ -282,13 +282,13 @@ private static function getDefinitelyEvaluatedExpressionInsideIf(PhpParser\Node\
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\Identical
) {
if ($stmt->left instanceof PhpParser\Node\Expr\ConstFetch
&& $stmt->left->name->parts === ['true']
&& $stmt->left->name->getParts() === ['true']
) {
return self::getDefinitelyEvaluatedExpressionInsideIf($stmt->right);
}

if ($stmt->right instanceof PhpParser\Node\Expr\ConstFetch
&& $stmt->right->name->parts === ['true']
&& $stmt->right->name->getParts() === ['true']
) {
return self::getDefinitelyEvaluatedExpressionInsideIf($stmt->left);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public static function analyze(
}

if ($switch_condition instanceof PhpParser\Node\Expr\ConstFetch
&& $switch_condition->name->parts === ['true']
&& $switch_condition->name->getParts() === ['true']
) {
$case_equality_expr = $case->cond;
} elseif (($switch_condition_type = $statements_analyzer->node_data->getType($switch_condition))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public static function analyze(
PhpParser\Node\Stmt\While_ $stmt,
Context $context
): ?bool {
$while_true = ($stmt->cond instanceof PhpParser\Node\Expr\ConstFetch && $stmt->cond->name->parts === ['true'])
$while_true = ($stmt->cond instanceof PhpParser\Node\Expr\ConstFetch
&& $stmt->cond->name->getParts() === ['true'])
|| (($t = $statements_analyzer->node_data->getType($stmt->cond))
&& $t->isAlwaysTruthy());

Expand Down
Loading

0 comments on commit 0981a15

Please sign in to comment.