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
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamStringTypeFromSprintfUseRector\Fixture;

class SkipMultiSprintfPossiblyFalsy
{
public function columnExpr($columnExpr, string $default): string
{
if (rand(0, 1)) {
return sprintf("hello %s", $columnExpr);
}

// $columnExpr can be anything empty: false | null | '' | 0 | '0' | []
if (empty($columnExpr)) {
return $default;
}

return sprintf("hello %s", $columnExpr);
}
}
78 changes: 41 additions & 37 deletions rules/TypeDeclaration/NodeAnalyzer/VariableInSprintfMaskMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,56 +32,60 @@ public function matchMask(ClassMethod|Function_ $functionLike, string $variableN
{
$funcCalls = $this->betterNodeFinder->findInstancesOfScoped((array) $functionLike->stmts, FuncCall::class);

foreach ($funcCalls as $funcCall) {
if (! $this->nodeNameResolver->isName($funcCall->name, 'sprintf')) {
if (count($funcCalls) !== 1) {
return false;
}

$funcCall = $funcCalls[0];

if (! $this->nodeNameResolver->isName($funcCall->name, 'sprintf')) {
return false;
}

if ($funcCall->isFirstClassCallable()) {
return false;
}

$args = $funcCall->getArgs();
if (count($args) < 2) {
return false;
}

/** @var Arg $messageArg */
$messageArg = array_shift($args);

$messageValue = $this->valueResolver->getValue($messageArg->value);
if (! is_string($messageValue)) {
return false;
}

// match all %s, %d types by position
$masks = Strings::match($messageValue, '#%[sd]#');

foreach ($args as $position => $arg) {
if (! $arg->value instanceof Variable) {
continue;
}

if ($funcCall->isFirstClassCallable()) {
if (! $this-> nodeNameResolver->isName($arg->value, $variableName)) {
continue;
}

$args = $funcCall->getArgs();
if (count($args) < 2) {
if (! isset($masks[$position])) {
continue;
}

/** @var Arg $messageArg */
$messageArg = array_shift($args);

$messageValue = $this->valueResolver->getValue($messageArg->value);
if (! is_string($messageValue)) {
$knownMaskOnPosition = $masks[$position];
if ($knownMaskOnPosition !== $mask) {
continue;
}

// match all %s, %d types by position
$masks = Strings::match($messageValue, '#%[sd]#');

foreach ($args as $position => $arg) {
if (! $arg->value instanceof Variable) {
continue;
}

if (! $this-> nodeNameResolver->isName($arg->value, $variableName)) {
continue;
}

if (! isset($masks[$position])) {
continue;
}

$knownMaskOnPosition = $masks[$position];
if ($knownMaskOnPosition !== $mask) {
continue;
}

$type = $this->nodeTypeResolver->getNativeType($arg->value);
if ($type instanceof MixedType && $type->getSubtractedType() instanceof UnionType) {
continue;
}

return true;
$type = $this->nodeTypeResolver->getNativeType($arg->value);
if ($type instanceof MixedType && $type->getSubtractedType() instanceof UnionType) {
continue;
}

return true;
}

return false;
Expand Down