Skip to content

Commit

Permalink
Prevent exceptions on UnionTypes (#2)
Browse files Browse the repository at this point in the history
Co-authored-by: Pascal Heidmann <pascal.heidmann@check24.de>

See: #2
  • Loading branch information
pascalheidmann authored Aug 18, 2022
1 parent 2834446 commit 43d7300
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Money\Currency;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeWithClassName;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -49,10 +49,10 @@ public function refactor(Node $node): ?Node

private function shouldSkip(MethodCall $node): bool
{
/** @var ObjectType $executedOn */
$executedOn = $this->nodeTypeResolver->getNativeType($node->var);

return $executedOn->getClassName() !== Currency::class
return !$executedOn instanceof TypeWithClassName
|| $executedOn->getClassName() !== Currency::class
|| $this->nodeNameResolver->getName($node->name) !== 'isAvailableWithin';
}
}
6 changes: 3 additions & 3 deletions src/Rule/MultiplyAndDivideByStringRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use PhpParser\Node\Scalar\String_;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Type\FloatType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeWithClassName;
use Rector\Core\Contract\Rector\AllowEmptyConfigurableRectorInterface;
use Rector\Core\PhpParser\AstResolver;
use Rector\Core\Rector\AbstractRector;
Expand Down Expand Up @@ -123,10 +123,10 @@ public function configure(array $configuration): void

private function shouldSkip(MethodCall $node): bool
{
/** @var ObjectType $executedOn */
$executedOn = $this->nodeTypeResolver->getNativeType($node->var);

return $executedOn->getClassName() !== Money::class
return !$executedOn instanceof TypeWithClassName
|| $executedOn->getClassName() !== Money::class
|| !$node->name instanceof Identifier
|| !in_array($node->name->toLowerString(), ['divide', 'multiply'], true);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Rector\Money\Tests\Rule\CurrencyAvailableWithinToCurrenciesContainsRector\Fixture;

use Money\Currencies\CurrencyList;
use Money\Currency;
use function mt_rand;

/**
* @return Currency|array|false
*/
function willReturnCurrencyOrSomethingDifferent() {
$random = mt_rand(0, 10);
if ($random % 3 === 0) {
return new Currency('EUR');
}
if ($random === 5) {
return ['some' => 'other', 'data'];
}
return false;
}

$collection = new CurrencyList([new Currency('PLN')]);
willReturnCurrencyOrSomethingDifferent()
->isAvailableWithin($collection);

?>

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

namespace Rector\Money\Tests\Rule\MultiplyAndDivideByStringRector\Fixture;

use Money\Currency;
use Money\Money;
use function mt_rand;

class UnionReturning
{
/**
* @return Money|array|false
*/
public static function foo()
{
$random = mt_rand(0, 10);
if ($random % 3 === 0) {
return new Money($random, new Currency('EUR'));
}
if ($random === 5) {
return ['some' => 'other', 'data'];
}
return false;
}

/**
* @return Money|array|false
*/
public function bar()
{
return self::foo();
}
}

$unionReturning = new UnionReturning();
$unionReturning->bar()->divide(1);
$unionReturning->bar()->multiply(2);
$unionReturning->bar()->divide(1.1);
$unionReturning->bar()->multiply(2.5);

UnionReturning::foo()->divide(3);
UnionReturning::foo()->multiply(4);
UnionReturning::foo()->divide(3.5);
UnionReturning::foo()->multiply(4.6);

0 comments on commit 43d7300

Please sign in to comment.