Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Downgrade PHP 7.0] Add string retype on object #6390

Merged
merged 1 commit into from
May 8, 2021
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
Expand Up @@ -22,6 +22,9 @@ class Fixture
*/
public function run($value)
{
if (is_object($value)) {
$value = (string) $value;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\Tests\DowngradePhp70\Rector\FunctionLike\DowngradeScalarTypeDeclarationRector\Fixture;

final class ObjectStringableParam
{
public function hasDefinition(string $id)
{
return $this->definitions[$id];
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp70\Rector\FunctionLike\DowngradeScalarTypeDeclarationRector\Fixture;

final class ObjectStringableParam
{
/**
* @param string $id
*/
public function hasDefinition($id)
{
if (is_object($id)) {
$id = (string) $id;
}
return $this->definitions[$id];
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
namespace Rector\DowngradePhp70\Rector\FunctionLike;

use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Cast\String_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\If_;
use PHPStan\Type\BooleanType;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
Expand Down Expand Up @@ -84,6 +89,20 @@ public function refactor(Node $node): ?Node
$node,
[StringType::class, IntegerType::class, BooleanType::class, FloatType::class]
);

$paramType = $this->getStaticType($param);
if ($paramType instanceof StringType) {
// add possible object with __toString() re-type to keep original behavior
// @see https://twitter.com/VotrubaT/status/1390974218108538887

/** @var string $variableName */
$variableName = $this->getName($param->var);

$if = $this->createObjetVariableStringCast($variableName);
$node->stmts = array_merge([$if], (array) $node->stmts);

return $node;
}
}

if (! $this->phpDocFromTypeDeclarationDecorator->decorateReturn($node)) {
Expand All @@ -92,4 +111,15 @@ public function refactor(Node $node): ?Node

return $node;
}

private function createObjetVariableStringCast(string $variableName): If_
{
$variable = new Variable($variableName);
$isObjectFuncCall = $this->nodeFactory->createFuncCall('is_object', [$variable]);

$if = new If_($isObjectFuncCall);
$assign = new Assign($variable, new String_($variable));
$if->stmts[] = new Expression($assign);
return $if;
}
}
3 changes: 3 additions & 0 deletions tests/Issues/Fixture/coariant_mixture.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ final class CoriantMixture
*/
private function parseArgument($token, array $all)
{
if (is_object($token)) {
$token = (string) $token;
}
reset($all);
if (($inputArgument = isset($all[$key = key($all)]) ? $all[$key = key($all)] : null) && 'command' === $inputArgument->getName()) {
}
Expand Down
3 changes: 3 additions & 0 deletions tests/Issues/Fixture/input_interface.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class ArgvInput extends Input
*/
private function parseArgument($token, array $all)
{
if (is_object($token)) {
$token = (string) $token;
}
reset($all);
if (($inputArgument = isset($all[$key = key($all)]) ? $all[$key = key($all)] : null) && 'command' === $inputArgument->getName()) {
}
Expand Down