Skip to content

Commit

Permalink
[Php80] Handle crash parent default empty array/string param on AddPa…
Browse files Browse the repository at this point in the history
…ramBasedOnParentClassMethodRector (#4833)

* [Php80] Handle crash parent default array param on AddParamBasedOnParentClassMethodRector

* [Php80] Handle crash parent default array param on AddParamBasedOnParentClassMethodRector

* Fixed 🎉

* Fixed 🎉

* Fixed 🎉

* Fixed 🎉

* ensure reprint

* test for non empty array and non empty stirng

* Really final touch: fixture rename

* add failing test crash on default int

* default int or float

* [ci-review] Rector Rectify

* float param fixture

* float param fix

* float param fixture

* final touch: handle empty string double quote

* Final touch: clean up fixture

* Really really final touch: fixture long array empty

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Aug 23, 2023
1 parent 4163578 commit 4b34887
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Fixture;

use Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Source\ParentWithParamWithDefaultValue;

class ExtendsParentDefaultEmptyArrayString extends ParentWithParamWithDefaultValue {
public function emptyArray() {
}

public function emptyArray2() {
}

public function emptyString() {
}

public function emptyString2() {
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Fixture;

use Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Source\ParentWithParamWithDefaultValue;

class ExtendsParentDefaultEmptyArrayString extends ParentWithParamWithDefaultValue {
public function emptyArray($default = []) {
}

public function emptyArray2($default = array()) {
}

public function emptyString($default = '') {
}

public function emptyString2($default = '') {
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Fixture;

use Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Source\ParentWithParamWithDefaultValue;

class ExtendsParentDefaultIntOrFloat extends ParentWithParamWithDefaultValue {
public function intParam() {
}

public function floatParam() {
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Fixture;

use Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Source\ParentWithParamWithDefaultValue;

class ExtendsParentDefaultIntOrFloat extends ParentWithParamWithDefaultValue {
public function intParam($default = 123) {
}

public function floatParam($default = 1.23) {
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Fixture;

use Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Source\ParentWithParamWithDefaultValue;

class ExtendsParentDefaultEmptyArrayString extends ParentWithParamWithDefaultValue {
public function nonEmptyArray() {
}

public function nonEmptyString() {
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Fixture;

use Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Source\ParentWithParamWithDefaultValue;

class ExtendsParentDefaultEmptyArrayString extends ParentWithParamWithDefaultValue {
public function nonEmptyArray($default = ['some data']) {
}

public function nonEmptyString($default = 'some value') {
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,36 @@ class ParentWithParamWithDefaultValue
public function execute($foo = true)
{
}

public function emptyArray($default = []) {
return implode('', $default);
}

public function emptyArray2($default = array()) {
return implode('', $default);
}

public function emptyString($default = '') {
return $default;
}

public function emptyString2($default = "") {
return $default;
}

public function nonEmptyArray($default = ['some data']) {
return implode('', $default);
}

public function nonEmptyString($default = 'some value') {
return $default;
}

public function intParam($default = 123) {
return $default;
}

public function floatParam($default = 1.23) {
return $default;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
use PhpParser\Node;
use PhpParser\Node\ComplexType;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Param;
use PhpParser\Node\Scalar\DNumber;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Reflection\MethodReflection;
use Rector\Core\PhpParser\AstResolver;
Expand Down Expand Up @@ -213,8 +217,7 @@ function (Node $subNode) use ($parentClassMethodParam): bool {
$paramDefault = $parentClassMethodParam->default;

if ($paramDefault instanceof Expr) {
$printParamDefault = $this->betterStandardPrinter->print($paramDefault);
$paramDefault = new ConstFetch(new Name($printParamDefault));
$paramDefault = $this->resolveParamDefault($paramDefault);
}

$paramName = $this->nodeNameResolver->getName($parentClassMethodParam);
Expand All @@ -239,6 +242,29 @@ function (Node $subNode) use ($parentClassMethodParam): bool {
return $node;
}

private function resolveParamDefault(Expr $expr): Expr
{
// re-create to avoid TokenStream error
$printParamDefault = $this->betterStandardPrinter->print($expr);
if ($printParamDefault === '[]') {
return new Array_([]);
}

if ($expr instanceof String_ && $expr->value === '') {
return new String_($expr->value);
}

if ($expr instanceof LNumber) {
return new LNumber($expr->value);
}

if ($expr instanceof DNumber) {
return new DNumber($expr->value);
}

return new ConstFetch(new Name($printParamDefault));
}

private function resolveParamType(Param $param): null|Identifier|Name|ComplexType
{
if ($param->type === null) {
Expand Down

0 comments on commit 4b34887

Please sign in to comment.