2021-01-17 15:43:47 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Arguments\NodeAnalyzer;
|
2021-01-17 15:43:47 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
|
|
use PhpParser\Node\Expr\StaticCall;
|
|
|
|
use PhpParser\Node\Name;
|
|
|
|
use Rector\Arguments\ValueObject\ArgumentAdder;
|
2023-12-23 19:44:26 +00:00
|
|
|
use Rector\Arguments\ValueObject\ArgumentAdderWithoutDefaultValue;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Enum\ObjectReference;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\NodeNameResolver\NodeNameResolver;
|
2021-01-17 15:43:47 +00:00
|
|
|
final class ArgumentAddingScope
|
|
|
|
{
|
2023-06-08 22:00:17 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2023-06-08 22:00:17 +00:00
|
|
|
* @var \Rector\NodeNameResolver\NodeNameResolver
|
|
|
|
*/
|
|
|
|
private $nodeNameResolver;
|
2021-01-17 15:43:47 +00:00
|
|
|
/**
|
2022-06-30 13:57:27 +00:00
|
|
|
* @api
|
2021-01-17 15:43:47 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public const SCOPE_PARENT_CALL = 'parent_call';
|
|
|
|
/**
|
2022-06-30 13:57:27 +00:00
|
|
|
* @api
|
2021-01-17 15:43:47 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public const SCOPE_METHOD_CALL = 'method_call';
|
|
|
|
/**
|
2022-06-30 13:57:27 +00:00
|
|
|
* @api
|
2021-01-17 15:43:47 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public const SCOPE_CLASS_METHOD = 'class_method';
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(NodeNameResolver $nodeNameResolver)
|
2021-01-17 15:43:47 +00:00
|
|
|
{
|
|
|
|
$this->nodeNameResolver = $nodeNameResolver;
|
|
|
|
}
|
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @param \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall $expr
|
2023-12-23 19:44:26 +00:00
|
|
|
* @param \Rector\Arguments\ValueObject\ArgumentAdder|\Rector\Arguments\ValueObject\ArgumentAdderWithoutDefaultValue $argumentAdder
|
2021-01-17 15:43:47 +00:00
|
|
|
*/
|
2023-12-23 19:44:26 +00:00
|
|
|
public function isInCorrectScope($expr, $argumentAdder) : bool
|
2021-01-17 15:43:47 +00:00
|
|
|
{
|
|
|
|
if ($argumentAdder->getScope() === null) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2021-01-17 15:43:47 +00:00
|
|
|
}
|
|
|
|
$scope = $argumentAdder->getScope();
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($expr instanceof StaticCall) {
|
|
|
|
if (!$expr->class instanceof Name) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2021-01-17 15:43:47 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($this->nodeNameResolver->isName($expr->class, ObjectReference::PARENT)) {
|
2021-01-17 15:43:47 +00:00
|
|
|
return $scope === self::SCOPE_PARENT_CALL;
|
|
|
|
}
|
|
|
|
return $scope === self::SCOPE_METHOD_CALL;
|
|
|
|
}
|
|
|
|
// MethodCall
|
|
|
|
return $scope === self::SCOPE_METHOD_CALL;
|
|
|
|
}
|
|
|
|
}
|