2020-05-02 21:25:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Rector\Naming\Naming;
|
|
|
|
|
|
|
|
use Nette\Utils\Strings;
|
2020-07-13 21:13:40 +00:00
|
|
|
use PhpParser\Node\Expr;
|
2020-06-29 20:01:32 +00:00
|
|
|
use PhpParser\Node\Expr\Assign;
|
2020-07-13 21:13:40 +00:00
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
2020-06-29 20:01:32 +00:00
|
|
|
use PhpParser\Node\Expr\New_;
|
2020-07-13 21:13:40 +00:00
|
|
|
use PhpParser\Node\Expr\StaticCall;
|
2020-06-29 20:01:32 +00:00
|
|
|
use PhpParser\Node\Expr\Variable;
|
|
|
|
use PhpParser\Node\Name;
|
2020-05-02 21:25:24 +00:00
|
|
|
use PhpParser\Node\Param;
|
|
|
|
use PhpParser\Node\Stmt\Property;
|
2020-07-27 09:26:41 +00:00
|
|
|
use PHPStan\Type\ArrayType;
|
2020-07-19 11:57:39 +00:00
|
|
|
use PHPStan\Type\MixedType;
|
2020-05-02 21:25:24 +00:00
|
|
|
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
|
|
|
use Rector\NodeNameResolver\NodeNameResolver;
|
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2020-07-19 11:57:39 +00:00
|
|
|
use Rector\NodeTypeResolver\NodeTypeResolver;
|
2020-06-29 20:01:32 +00:00
|
|
|
use Rector\PHPStan\Type\FullyQualifiedObjectType;
|
2020-05-02 21:25:24 +00:00
|
|
|
use Rector\StaticTypeMapper\StaticTypeMapper;
|
|
|
|
|
|
|
|
final class ExpectedNameResolver
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var NodeNameResolver
|
|
|
|
*/
|
|
|
|
private $nodeNameResolver;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var PropertyNaming
|
|
|
|
*/
|
|
|
|
private $propertyNaming;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var StaticTypeMapper
|
|
|
|
*/
|
|
|
|
private $staticTypeMapper;
|
|
|
|
|
2020-07-13 21:13:40 +00:00
|
|
|
/**
|
2020-07-19 11:57:39 +00:00
|
|
|
* @var NodeTypeResolver
|
2020-07-13 21:13:40 +00:00
|
|
|
*/
|
2020-07-19 11:57:39 +00:00
|
|
|
private $nodeTypeResolver;
|
2020-07-13 21:13:40 +00:00
|
|
|
|
2020-05-02 21:25:24 +00:00
|
|
|
public function __construct(
|
|
|
|
NodeNameResolver $nodeNameResolver,
|
2020-07-26 07:49:22 +00:00
|
|
|
NodeTypeResolver $nodeTypeResolver,
|
2020-05-02 21:25:24 +00:00
|
|
|
PropertyNaming $propertyNaming,
|
2020-07-26 07:49:22 +00:00
|
|
|
StaticTypeMapper $staticTypeMapper
|
2020-05-02 21:25:24 +00:00
|
|
|
) {
|
|
|
|
$this->nodeNameResolver = $nodeNameResolver;
|
|
|
|
$this->propertyNaming = $propertyNaming;
|
|
|
|
$this->staticTypeMapper = $staticTypeMapper;
|
2020-07-19 11:57:39 +00:00
|
|
|
$this->nodeTypeResolver = $nodeTypeResolver;
|
2020-05-02 21:25:24 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 22:06:12 +00:00
|
|
|
public function resolveForPropertyIfNotYet(Property $property): ?string
|
2020-05-02 21:25:24 +00:00
|
|
|
{
|
2020-05-02 22:06:12 +00:00
|
|
|
$expectedName = $this->resolveForProperty($property);
|
|
|
|
if ($expectedName === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-05-02 21:25:24 +00:00
|
|
|
|
2020-05-03 11:04:19 +00:00
|
|
|
/** @var string $propertyName */
|
|
|
|
$propertyName = $this->nodeNameResolver->getName($property);
|
|
|
|
if ($this->endsWith($propertyName, $expectedName)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-05-02 22:06:12 +00:00
|
|
|
if ($this->nodeNameResolver->isName($property, $expectedName)) {
|
2020-05-02 21:25:24 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $expectedName;
|
|
|
|
}
|
|
|
|
|
2020-05-02 22:06:12 +00:00
|
|
|
public function resolveForParamIfNotYet(Param $param): ?string
|
2020-05-02 21:25:24 +00:00
|
|
|
{
|
2020-05-02 22:06:12 +00:00
|
|
|
$expectedName = $this->resolveForParam($param);
|
2020-05-02 21:25:24 +00:00
|
|
|
if ($expectedName === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var string $currentName */
|
|
|
|
$currentName = $this->nodeNameResolver->getName($param->var);
|
|
|
|
if ($currentName === $expectedName) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->endsWith($currentName, $expectedName)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $expectedName;
|
|
|
|
}
|
|
|
|
|
2020-05-02 22:06:12 +00:00
|
|
|
public function resolveForParam(Param $param): ?string
|
|
|
|
{
|
|
|
|
// nothing to verify
|
|
|
|
if ($param->type === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$staticType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($param->type);
|
|
|
|
return $this->propertyNaming->getExpectedNameFromType($staticType);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function resolveForProperty(Property $property): ?string
|
|
|
|
{
|
|
|
|
/** @var PhpDocInfo|null $phpDocInfo */
|
|
|
|
$phpDocInfo = $property->getAttribute(AttributeKey::PHP_DOC_INFO);
|
|
|
|
if ($phpDocInfo === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->propertyNaming->getExpectedNameFromType($phpDocInfo->getVarType());
|
|
|
|
}
|
|
|
|
|
2020-06-29 20:01:32 +00:00
|
|
|
public function resolveForAssignNonNew(Assign $assign): ?string
|
|
|
|
{
|
|
|
|
if ($assign->expr instanceof New_) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $assign->var instanceof Variable) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var Variable $variable */
|
|
|
|
$variable = $assign->var;
|
|
|
|
|
|
|
|
return $this->nodeNameResolver->getName($variable);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function resolveForAssignNew(Assign $assign): ?string
|
|
|
|
{
|
|
|
|
if (! $assign->expr instanceof New_) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $assign->var instanceof Variable) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var New_ $new */
|
|
|
|
$new = $assign->expr;
|
|
|
|
if (! $new->class instanceof Name) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$className = $this->nodeNameResolver->getName($new->class);
|
|
|
|
if ($className === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fullyQualifiedObjectType = new FullyQualifiedObjectType($className);
|
|
|
|
|
|
|
|
return $this->propertyNaming->getExpectedNameFromType($fullyQualifiedObjectType);
|
|
|
|
}
|
|
|
|
|
2020-07-19 12:30:07 +00:00
|
|
|
/**
|
2020-07-19 16:11:59 +00:00
|
|
|
* @param MethodCall|StaticCall|FuncCall $expr
|
2020-07-19 12:30:07 +00:00
|
|
|
*/
|
2020-07-19 17:54:00 +00:00
|
|
|
public function resolveForCall(Expr $expr): ?string
|
2020-07-13 21:13:40 +00:00
|
|
|
{
|
2020-07-19 17:54:00 +00:00
|
|
|
if ($this->isDynamicNameCall($expr)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-07-19 16:11:59 +00:00
|
|
|
$name = $this->nodeNameResolver->getName($expr->name);
|
2020-07-13 21:13:40 +00:00
|
|
|
if ($name === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-07-19 16:11:59 +00:00
|
|
|
$returnedType = $this->nodeTypeResolver->getStaticType($expr);
|
2020-07-27 09:26:41 +00:00
|
|
|
|
|
|
|
if ($returnedType instanceof ArrayType) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-07-19 11:57:39 +00:00
|
|
|
if ($returnedType instanceof MixedType) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$expectedName = $this->propertyNaming->getExpectedNameFromType($returnedType);
|
|
|
|
if ($expectedName !== null) {
|
|
|
|
return $expectedName;
|
|
|
|
}
|
|
|
|
|
2020-07-19 13:47:23 +00:00
|
|
|
// call with args can return different value, so skip there if not sure about the type
|
2020-07-19 16:11:59 +00:00
|
|
|
if (count($expr->args) > 0) {
|
2020-07-19 13:47:23 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-07-13 21:13:40 +00:00
|
|
|
// @see https://regex101.com/r/hnU5pm/2/
|
2020-07-19 13:47:23 +00:00
|
|
|
$matches = Strings::match($name, '#^get([A-Z].+)#');
|
2020-07-13 21:13:40 +00:00
|
|
|
if ($matches === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lcfirst($matches[1]);
|
|
|
|
}
|
|
|
|
|
2020-05-02 21:25:24 +00:00
|
|
|
/**
|
|
|
|
* Ends with ucname
|
|
|
|
* Starts with adjective, e.g. (Post $firstPost, Post $secondPost)
|
|
|
|
*/
|
|
|
|
private function endsWith(string $currentName, string $expectedName): bool
|
|
|
|
{
|
2020-05-03 11:04:19 +00:00
|
|
|
$suffixNamePattern = '#\w+' . ucfirst($expectedName) . '#';
|
|
|
|
return (bool) Strings::match($currentName, $suffixNamePattern);
|
2020-05-02 21:25:24 +00:00
|
|
|
}
|
2020-07-19 17:54:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param MethodCall|StaticCall|FuncCall $expr
|
|
|
|
*/
|
|
|
|
private function isDynamicNameCall(Expr $expr): bool
|
|
|
|
{
|
|
|
|
if ($expr->name instanceof StaticCall) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($expr->name instanceof MethodCall) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $expr->name instanceof FuncCall;
|
|
|
|
}
|
2020-05-02 21:25:24 +00:00
|
|
|
}
|