Skip to content

Commit f162f3a

Browse files
committed
Utilize array_last()
1 parent 8c871fc commit f162f3a

16 files changed

+114
-33
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"symfony/polyfill-php80": "^1.23",
4848
"symfony/polyfill-php81": "^1.27",
4949
"symfony/polyfill-php83": "^1.33",
50+
"symfony/polyfill-php85": "^1.33",
5051
"symfony/process": "^5.4.3",
5152
"symfony/service-contracts": "^2.5.0",
5253
"symfony/string": "^5.4.3"

composer.lock

Lines changed: 81 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Analyser/MutatingScope.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3779,7 +3779,7 @@ private function enterAnonymousFunctionWithoutReflection(
37793779
if (isset($callableParameters[$i])) {
37803780
$parameterType = self::intersectButNotNever($parameterType, $callableParameters[$i]->getType());
37813781
} elseif (count($callableParameters) > 0) {
3782-
$lastParameter = $callableParameters[count($callableParameters) - 1];
3782+
$lastParameter = array_last($callableParameters);
37833783
if ($lastParameter->isVariadic()) {
37843784
$parameterType = self::intersectButNotNever($parameterType, $lastParameter->getType());
37853785
} else {
@@ -3972,7 +3972,7 @@ private function enterArrowFunctionWithoutReflection(Expr\ArrowFunction $arrowFu
39723972
if (isset($callableParameters[$i])) {
39733973
$parameterType = self::intersectButNotNever($parameterType, $callableParameters[$i]->getType());
39743974
} elseif (count($callableParameters) > 0) {
3975-
$lastParameter = $callableParameters[count($callableParameters) - 1];
3975+
$lastParameter = array_last($callableParameters);
39763976
if ($lastParameter->isVariadic()) {
39773977
$parameterType = self::intersectButNotNever($parameterType, $lastParameter->getType());
39783978
} else {

src/Analyser/NodeScopeResolver.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,7 +1481,7 @@ private function processStmtNode(
14811481

14821482
$bodyScope = $initScope;
14831483
$isIterableAtLeastOnce = TrinaryLogic::createYes();
1484-
$lastCondExpr = $stmt->cond[count($stmt->cond) - 1] ?? null;
1484+
$lastCondExpr = array_last($stmt->cond) ?? null;
14851485
foreach ($stmt->cond as $condExpr) {
14861486
$condResult = $this->processExprNode($stmt, $condExpr, $bodyScope, static function (): void {
14871487
}, ExpressionContext::createDeep());
@@ -5239,7 +5239,7 @@ private function processArgs(
52395239
}
52405240
$parameter = $parameters[$i];
52415241
} elseif (count($parameters) > 0 && $parametersAcceptor->isVariadic()) {
5242-
$lastParameter = $parameters[count($parameters) - 1];
5242+
$lastParameter = array_last($parameters);
52435243
$assignByReference = $lastParameter->passedByReference()->createsNewVariable();
52445244
$parameterType = $lastParameter->getType();
52455245

@@ -5432,7 +5432,7 @@ private function processArgs(
54325432
if (isset($parameters[$i])) {
54335433
$currentParameter = $parameters[$i];
54345434
} elseif (count($parameters) > 0 && $parametersAcceptor->isVariadic()) {
5435-
$currentParameter = $parameters[count($parameters) - 1];
5435+
$currentParameter = array_last($parameters);
54365436
}
54375437

54385438
if ($currentParameter !== null) {

src/Analyser/TypeSpecifier.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1517,7 +1517,7 @@ private function specifyTypesFromAsserts(TypeSpecifierContext $context, Expr\Cal
15171517
} elseif (isset($parameters[$i])) {
15181518
$paramName = $parameters[$i]->getName();
15191519
} elseif (count($parameters) > 0 && $parametersAcceptor->isVariadic()) {
1520-
$lastParameter = $parameters[count($parameters) - 1];
1520+
$lastParameter = array_last($parameters);
15211521
$paramName = $lastParameter->getName();
15221522
} else {
15231523
continue;

src/Fixable/PhpPrinter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ protected function pCommaSeparated(array $nodes): string
2424
if (count($nodes) === 0) {
2525
return $result;
2626
}
27-
$last = $nodes[count($nodes) - 1];
27+
$last = array_last($nodes);
2828

2929
$trailingComma = $last->getAttribute(self::FUNC_ARGS_TRAILING_COMMA_ATTRIBUTE);
3030
if ($trailingComma === false) {

src/Parser/LastConditionVisitor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function enterNode(Node $node): ?Node
8787
}
8888

8989
$if = $statements[$statementCount - 2];
90-
$cond = count($if->elseifs) > 0 ? $if->elseifs[count($if->elseifs) - 1]->cond : $if->cond;
90+
$cond = count($if->elseifs) > 0 ? array_last($if->elseifs)->cond : $if->cond;
9191
$cond->setAttribute(self::ATTRIBUTE_NAME, true);
9292
}
9393

src/Parser/TryCatchTypeVisitor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function enterNode(Node $node): ?Node
3131
{
3232
if ($node instanceof Node\Stmt || $node instanceof Node\Expr\Match_) {
3333
if (count($this->typeStack) > 0) {
34-
$node->setAttribute(self::ATTRIBUTE_NAME, $this->typeStack[count($this->typeStack) - 1]);
34+
$node->setAttribute(self::ATTRIBUTE_NAME, array_last($this->typeStack));
3535
}
3636
}
3737

src/Parser/VariadicMethodsVisitor.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use PHPStan\Reflection\ParametersAcceptor;
1212
use function array_key_exists;
1313
use function array_pop;
14-
use function count;
1514
use function in_array;
1615
use function sprintf;
1716

@@ -74,15 +73,15 @@ public function enterNode(Node $node): ?Node
7473
$this->inMethodStack[] = $node->name->name;
7574
}
7675

77-
$lastMethod = $this->inMethodStack[count($this->inMethodStack) - 1] ?? null;
76+
$lastMethod = array_last($this->inMethodStack) ?? null;
7877

7978
if (
8079
$lastMethod !== null
8180
&& $node instanceof Node\Expr\FuncCall
8281
&& $node->name instanceof Name
8382
&& in_array((string) $node->name, ParametersAcceptor::VARIADIC_FUNCTIONS, true)
8483
) {
85-
$lastClass = $this->classStack[count($this->classStack) - 1] ?? null;
84+
$lastClass = array_last($this->classStack) ?? null;
8685
if ($lastClass !== null) {
8786
if (
8887
!array_key_exists($lastClass, $this->variadicMethods)
@@ -101,8 +100,8 @@ public function enterNode(Node $node): ?Node
101100
public function leaveNode(Node $node): ?Node
102101
{
103102
if ($node instanceof ClassMethod) {
104-
$lastClass = $this->classStack[count($this->classStack) - 1] ?? null;
105-
$lastMethod = $this->inMethodStack[count($this->inMethodStack) - 1] ?? null;
103+
$lastClass = array_last($this->classStack) ?? null;
104+
$lastMethod = array_last($this->inMethodStack) ?? null;
106105
if ($lastClass !== null && $lastMethod !== null) {
107106
$this->variadicMethods[$lastClass][$lastMethod] ??= false;
108107
}

src/Reflection/AttributeReflectionFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private function fromNameAndArgumentExpressions(string $name, array $arguments,
107107
continue;
108108
}
109109
if (count($parameters) > 0) {
110-
$lastParameter = $parameters[count($parameters) - 1];
110+
$lastParameter = array_last($parameters);
111111
if ($lastParameter->isVariadic()) {
112112
$parameterName = $lastParameter->getName();
113113
if (array_key_exists($parameterName, $namedArgTypes)) {

0 commit comments

Comments
 (0)