Skip to content

Commit

Permalink
[cs] add static lambda rule [closes #2514]
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jun 21, 2022
1 parent 0fb4f5b commit 83f2b16
Show file tree
Hide file tree
Showing 53 changed files with 71 additions and 68 deletions.
2 changes: 1 addition & 1 deletion bin/generate-changelog.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$commitLines = $this->resolveCommitLinesFromToHashes($fromCommit, $toCommit);

$commits = array_map(function (string $line): array {
$commits = array_map(static function (string $line): array {
[$hash, $message] = explode(' ', $line, 2);
return [
self::HASH => $hash,
Expand Down
3 changes: 3 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis\AssignmentInConditionSniff;
use PhpCsFixer\Fixer\FunctionNotation\FunctionTypehintSpaceFixer;
use PhpCsFixer\Fixer\FunctionNotation\StaticLambdaFixer;
use PhpCsFixer\Fixer\Phpdoc\GeneralPhpdocAnnotationRemoveFixer;
use PhpCsFixer\Fixer\Phpdoc\NoSuperfluousPhpdocTagsFixer;
use PhpCsFixer\Fixer\Phpdoc\PhpdocNoEmptyReturnFixer;
Expand Down Expand Up @@ -50,6 +51,8 @@
],
]);

$ecsConfig->rule(StaticLambdaFixer::class);

$ecsConfig->skip([
'*/Source/*',
'*/Fixture/*',
Expand Down
2 changes: 1 addition & 1 deletion packages/BetterPhpDocParser/Comment/CommentsMerger.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private function collectChildrenComments(Node $node): array
{
$childrenComments = [];

$this->simpleCallableNodeTraverser->traverseNodesWithCallable($node, function (Node $node) use (
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($node, static function (Node $node) use (
&$childrenComments
) {
$comments = $node->getComments();
Expand Down
2 changes: 1 addition & 1 deletion packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function getTagsByName(string $name): array
$tags = $this->phpDocNode->getTags();
$name = $this->annotationNaming->normalizeName($name);

$tags = array_filter($tags, fn (PhpDocTagNode $tag): bool => $tag->name === $name);
$tags = array_filter($tags, static fn (PhpDocTagNode $tag): bool => $tag->name === $name);

$tags = array_values($tags);
return array_values($tags);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function removeTagValueFromNode(PhpDocInfo $phpDocInfo, Node $desiredNode
$phpDocNode = $phpDocInfo->getPhpDocNode();

$phpDocNodeTraverser = new PhpDocNodeTraverser();
$phpDocNodeTraverser->traverseWithCallable($phpDocNode, '', function ($node) use (
$phpDocNodeTraverser->traverseWithCallable($phpDocNode, '', static function ($node) use (
$desiredNode,
$phpDocInfo
): ?int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function findByType(PhpDocNode $phpDocNode, string $desiredType): array

$foundNodes = [];

$phpDocNodeTraverser->traverseWithCallable($phpDocNode, '', function ($node) use (
$phpDocNodeTraverser->traverseWithCallable($phpDocNode, '', static function ($node) use (
&$foundNodes,
$desiredType
): Node {
Expand Down
2 changes: 1 addition & 1 deletion packages/Config/RectorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function ruleWithConfiguration(string $rectorClass, array $configuration)
$services = $this->services();

// decorate with value object inliner so Symfony understands, see https://getrector.org/blog/2020/09/07/how-to-inline-value-object-in-symfony-php-config
array_walk_recursive($configuration, function (&$value) {
array_walk_recursive($configuration, static function (&$value) {
if (is_object($value)) {
$value = ValueObjectInliner::inline($value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private function getVariableUsages(Variable $variable): array
return [];
}

return $this->betterNodeFinder->find((array) $scope->stmts, function (Node $node) use ($variable): bool {
return $this->betterNodeFinder->find((array) $scope->stmts, static function (Node $node) use ($variable): bool {
if (! $node instanceof Variable) {
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/NodeTypeResolver/PHPStan/TypeHasher.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function createTypeHash(Type $type): string
$type = $this->normalizeObjectType($type);

// normalize iterable
$type = TypeTraverser::map($type, function (Type $currentType, callable $traverseCallback): Type {
$type = TypeTraverser::map($type, static function (Type $currentType, callable $traverseCallback): Type {
if (! $currentType instanceof ObjectType) {
return $traverseCallback($currentType);
}
Expand Down Expand Up @@ -96,7 +96,7 @@ private function createUnionTypeHash(UnionType $unionType): string
// change alias to non-alias
$normalizedUnionType = TypeTraverser::map(
$normalizedUnionType,
function (Type $type, callable $callable): Type {
static function (Type $type, callable $callable): Type {
if (! $type instanceof AliasedObjectType && ! $type instanceof ShortenedObjectType) {
return $callable($type);
}
Expand All @@ -110,7 +110,7 @@ function (Type $type, callable $callable): Type {

private function normalizeObjectType(Type $type): Type
{
return TypeTraverser::map($type, function (Type $currentType, callable $traverseCallback): Type {
return TypeTraverser::map($type, static function (Type $currentType, callable $traverseCallback): Type {
if ($currentType instanceof ShortenedObjectType) {
return new FullyQualifiedObjectType($currentType->getFullyQualifiedName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ private function areArrayUnionConstantEqualTypes(Type $firstType, Type $secondTy

private function normalizeConstantBooleanType(Type $type): Type
{
return TypeTraverser::map($type, function (Type $type, callable $callable): Type {
return TypeTraverser::map($type, static function (Type $type, callable $callable): Type {
if ($type instanceof ConstantBooleanType) {
return new BooleanType();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ private function filterOutNativeClassReflections(array $classReflections): array
{
return array_filter(
$classReflections,
fn (ClassReflection $classReflection): bool => ! $classReflection->isBuiltin()
static fn (ClassReflection $classReflection): bool => ! $classReflection->isBuiltin()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private function isLeftPartOfAssign(Expr $expr): bool

return ! (bool) $this->betterNodeFinder->findFirst(
$parentAssign->var,
fn (Node $node): bool => $node === $expr
static fn (Node $node): bool => $node === $expr
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private function shouldSkipChaoticClassMethods(ClassMethod $classMethod): bool

private function hasClassMethodExprReturn(ClassMethod $classMethod): bool
{
return (bool) $this->betterNodeFinder->findFirst((array) $classMethod->stmts, function (Node $node): bool {
return (bool) $this->betterNodeFinder->findFirst((array) $classMethod->stmts, static function (Node $node): bool {
if (! $node instanceof Return_) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion rules/CodeQuality/NodeAnalyzer/ForAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function isArrayWithKeyValueNameUnsetted(For_ $for): bool
{
return (bool) $this->betterNodeFinder->findFirst(
$for->stmts,
function (Node $node): bool {
static function (Node $node): bool {
/** @var Node $parent */
$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
if (! $parent instanceof Unset_) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private function shouldSkipExactlyReturnDateTime(ClassMethod $classMethod): bool
{
$return = $this->betterNodeFinder->findFirstInFunctionLikeScoped(
$classMethod,
fn (Node $node): bool => $node instanceof Return_
static fn (Node $node): bool => $node instanceof Return_
);
if (! $return instanceof Return_) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function provideMinPhpVersion(): int
*/
private function matchReturnOrAssignNode(Foreach_ $foreach): ?Node
{
return $this->foreachManipulator->matchOnlyStmt($foreach, function (Node $node): ?Node {
return $this->foreachManipulator->matchOnlyStmt($foreach, static function (Node $node): ?Node {
if (! $node instanceof If_) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private function processIdenticalAndNotIdentical(Identical $identical): ?Node
{
$twoNodeMatch = $this->binaryOpManipulator->matchFirstAndSecondConditionNode(
$identical,
fn (Node $binaryOp): bool => $binaryOp instanceof Identical || $binaryOp instanceof NotIdentical,
static fn (Node $binaryOp): bool => $binaryOp instanceof Identical || $binaryOp instanceof NotIdentical,
fn (Node $binaryOp): bool => $this->valueResolver->isTrueOrFalse($binaryOp)
);

Expand Down
2 changes: 1 addition & 1 deletion rules/CodingStyle/ClassNameImport/AliasUsesResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function resolveFromStmts(array $stmts): array
{
$aliasedUses = [];

$this->useImportsTraverser->traverserStmts($stmts, function (
$this->useImportsTraverser->traverserStmts($stmts, static function (
UseUse $useUse,
string $name
) use (&$aliasedUses): void {
Expand Down
2 changes: 1 addition & 1 deletion rules/CodingStyle/ClassNameImport/ShortNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private function resolveFromStmtsDocBlocks(array $stmts): array
$phpDocNodeTraverser->traverseWithCallable(
$phpDocInfo->getPhpDocNode(),
'',
function ($node) use (&$shortNames) {
static function ($node) use (&$shortNames) {
if ($node instanceof PhpDocTagNode) {
$shortName = trim($node->name, '@');
if (StringUtils::isMatch($shortName, self::BIG_LETTER_START_REGEX)) {
Expand Down
4 changes: 2 additions & 2 deletions rules/CodingStyle/ClassNameImport/UsedImportsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function resolveForStmts(array $stmts): array
$usedImports[] = new FullyQualifiedObjectType($className);
}

$this->useImportsTraverser->traverserStmts($stmts, function (
$this->useImportsTraverser->traverserStmts($stmts, static function (
UseUse $useUse,
string $name
) use (&$usedImports): void {
Expand All @@ -80,7 +80,7 @@ public function resolveFunctionImportsForStmts(array $stmts): array
{
$usedFunctionImports = [];

$this->useImportsTraverser->traverserStmtsForFunctions($stmts, function (
$this->useImportsTraverser->traverserStmtsForFunctions($stmts, static function (
UseUse $useUse,
string $name
) use (&$usedFunctionImports): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function refactor(Node $node): ?Node
$newVariableName = Strings::replace(
lcfirst($typeShortName),
self::STARTS_WITH_ABBREVIATION_REGEX,
function (array $matches): string {
static function (array $matches): string {
$output = '';

$output .= isset($matches[1]) ? strtolower((string) $matches[1]) : '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function configure(array $configuration): void
Assert::allString($configuration);

$this->annotationsToConsiderForInlining = array_map(
fn (string $annotation): string => '@' . ltrim($annotation, '@'),
static fn (string $annotation): string => '@' . ltrim($annotation, '@'),
$configuration
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function getParts(String_ $string, array $classNames): array
// @see https://regex101.com/r/8nGS0F/1
$parts = Strings::split($string->value, '#(' . implode('|', $quotedClassNames) . ')#');

return array_filter($parts, fn (string $className): bool => $className !== '');
return array_filter($parts, static fn (string $className): bool => $className !== '');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private function sortByStart(array $nodesByTypeAndPosition): array
{
usort(
$nodesByTypeAndPosition,
fn (VariableNodeUse $firstVariableNodeUse, VariableNodeUse $secondVariableNodeUse): int => $firstVariableNodeUse->getStartTokenPosition() <=> $secondVariableNodeUse->getStartTokenPosition()
static fn (VariableNodeUse $firstVariableNodeUse, VariableNodeUse $secondVariableNodeUse): int => $firstVariableNodeUse->getStartTokenPosition() <=> $secondVariableNodeUse->getStartTokenPosition()
);
return $nodesByTypeAndPosition;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ private function getArrayItemsWithDuplicatedKey(Array_ $array): array
private function filterItemsWithSameKey(array $arrayItemsByKeys): array
{
/** @var ArrayItem[][] $arrayItemsByKeys */
$arrayItemsByKeys = array_filter($arrayItemsByKeys, fn (array $arrayItems): bool => count($arrayItems) > 1);
$arrayItemsByKeys = array_filter($arrayItemsByKeys, static fn (array $arrayItems): bool => count($arrayItems) > 1);

return array_filter($arrayItemsByKeys, fn (array $arrayItems): bool => count($arrayItems) > 1);
return array_filter($arrayItemsByKeys, static fn (array $arrayItems): bool => count($arrayItems) > 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private function shouldSkip(Assign $assign): bool

return (bool) $this->betterNodeFinder->findFirstNext(
$assign,
fn (Node $node): bool => $node instanceof Variable
static fn (Node $node): bool => $node instanceof Variable
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private function replaceVariablesWithPropertyFetch(

$this->traverseNodesWithCallable(
$stmtsAware,
function (Node $node) use ($variableUsages, $propertyFetch): ?PropertyFetch {
static function (Node $node) use ($variableUsages, $propertyFetch): ?PropertyFetch {
if (! in_array($node, $variableUsages, true)) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function refactor(Node $node): ?Node

$this->nodesToAddCollector->addNodeBeforeNode($expression, $node, $this->file->getSmartFileInfo());

$this->traverseNodesWithCallable($node, function (Node $subNode) use ($selfVariable): ?Closure {
$this->traverseNodesWithCallable($node, static function (Node $subNode) use ($selfVariable): ?Closure {
if (! $subNode instanceof Closure) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private function createReflectionInstantiation(New_ $new, array $args): ?Expr
/** @var Array_ $array */
$array = $unpackedArgs[0]->value;
$arrayItems = array_filter($array->items);
$new->args = array_map(fn (ArrayItem $item): Arg => new Arg($item->value), $arrayItems);
$new->args = array_map(static fn (ArrayItem $item): Arg => new Arg($item->value), $arrayItems);

return $new;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private function applyArrayFilterUseBoth(array $args, Closure $closure, Variable
]);
$stmts = [];

$this->simpleCallableNodeTraverser->traverseNodesWithCallable($closure->stmts, function (Node $subNode) use (
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($closure->stmts, static function (Node $subNode) use (
$variable,
$key,
$value,
Expand Down Expand Up @@ -184,7 +184,7 @@ private function applyArrayFilterUseKey(array $args, Closure $closure, Variable
$foreach = new Foreach_($funcCall, $key);
$stmts = [];

$this->simpleCallableNodeTraverser->traverseNodesWithCallable($closure->stmts, function (Node $subNode) use (
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($closure->stmts, static function (Node $subNode) use (
$variable,
$key,
$arrayValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
$match = $this->betterNodeFinder->findFirst($node, fn (Node $subNode): bool => $subNode instanceof Match_);
$match = $this->betterNodeFinder->findFirst($node, static fn (Node $subNode): bool => $subNode instanceof Match_);
if (! $match instanceof Match_) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private function hasThrowInAssignExpr(Assign $assign): bool
{
return (bool) $this->betterNodeFinder->findFirst(
$assign->expr,
fn (Node $node): bool => $node instanceof Throw_
static fn (Node $node): bool => $node instanceof Throw_
);
}

Expand Down
2 changes: 1 addition & 1 deletion rules/EarlyReturn/NodeFactory/InvertedIfFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private function getNextReturnExpr(If_ $if): ?Node
{
return $this->betterNodeFinder->findFirstNext(
$if,
fn (Node $node): bool => $node instanceof Return_ && $node->expr instanceof Expr
static fn (Node $node): bool => $node instanceof Return_ && $node->expr instanceof Expr
);
}

Expand Down
4 changes: 2 additions & 2 deletions rules/Naming/Naming/UseImportsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function resolveForNode(Node $node): array

return array_filter(
$namespace->stmts,
fn (Stmt $stmt): bool => $stmt instanceof Use_ || $stmt instanceof GroupUse
static fn (Stmt $stmt): bool => $stmt instanceof Use_ || $stmt instanceof GroupUse
);
}

Expand All @@ -51,7 +51,7 @@ public function resolveBareUsesForNode(Node $node): array
return [];
}

return array_filter($namespace->stmts, fn (Stmt $stmt): bool => $stmt instanceof Use_);
return array_filter($namespace->stmts, static fn (Stmt $stmt): bool => $stmt instanceof Use_);
}

public function resolvePrefix(Use_|GroupUse $use): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,6 @@ private function getAssignsOfNew(ClassMethod $classMethod): array
/** @var Assign[] $assigns */
$assigns = $this->betterNodeFinder->findInstanceOf((array) $classMethod->stmts, Assign::class);

return array_filter($assigns, fn (Assign $assign): bool => $assign->expr instanceof New_);
return array_filter($assigns, static fn (Assign $assign): bool => $assign->expr instanceof New_);
}
}
2 changes: 1 addition & 1 deletion rules/PSR4/Composer/PSR4AutoloadPathsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private function removeEmptyNamespaces(array $psr4Autoloads): array
{
return array_filter(
$psr4Autoloads,
fn (string $psr4Autoload): bool => $psr4Autoload !== '',
static fn (string $psr4Autoload): bool => $psr4Autoload !== '',
ARRAY_FILTER_USE_KEY
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private function hasNamespaceInPreviousNamespace(Namespace_ $namespace): bool
{
return (bool) $this->betterNodeFinder->findFirstPrevious(
$namespace,
fn (Node $node): bool => $node instanceof Namespace_
static fn (Node $node): bool => $node instanceof Namespace_
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ private function findNonAnonymousClassLikes(Node $node): array
{
$classLikes = $this->betterNodeFinder->findInstanceOf([$node], ClassLike::class);

return array_filter($classLikes, function (ClassLike $classLike): bool {
return array_filter($classLikes, static function (ClassLike $classLike): bool {
if (! $classLike instanceof Class_) {
return true;
}
Expand Down
Loading

0 comments on commit 83f2b16

Please sign in to comment.