Skip to content
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 @@ -25,11 +25,7 @@ public function getShortName(): string
public function toAttributeString(): string
{
$items = $this->createAttributeItems();
$items = $this->filterOutMissingItems($items);

$content = $this->printPhpAttributeItems($items);

return $this->printAttributeContent($content);
return $this->printItemsToAttributeString($items);
}

private function createAttributeItems(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@ public function getShortName(): string

public function toAttributeString(): string
{
$items = $this->filterOutMissingItems($this->items);
$items = $this->completeItemsQuotes($items);
$items = $this->createAttributeItems();
return $this->printItemsToAttributeString($items);
}

private function createAttributeItems(): array
{
$items = $this->items;

foreach ($items as $key => $value) {
if ($key !== 'unique') {
Expand All @@ -49,8 +54,6 @@ public function toAttributeString(): string
$items[$key] = 'ORM\Column::UNIQUE';
}

$content = $this->printPhpAttributeItems($items);

return $this->printAttributeContent($content);
return $items;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public function getShortName(): string

public function toAttributeString(): string
{
// @todo add strategy
return $this->printAttributeContent();
return $this->printItemsToAttributeString($this->items);
}

public function getSilentKey(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public function getShortName(): string

public function toAttributeString(): string
{
return $this->printAttributeContent();
return $this->printItemsToAttributeString($this->items);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ public function changeShortName(string $shortName): void

public function toAttributeString(): string
{
$items = $this->filterOutMissingItems($this->items);
$items = $this->completeItemsQuotes($items);
return $this->printItemsToAttributeString($this->createAttributeItems());
}

private function createAttributeItems(): array
{
$items = $this->items;

// specific for attributes
foreach ($items as $key => $value) {
Expand All @@ -70,8 +74,6 @@ public function toAttributeString(): string
$items[$key] = 'ORM\JoinColumn::UNIQUE';
}

$content = $this->printPhpAttributeItems($items);

return $this->printAttributeContent($content);
return $items;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function toAttributeString(): string

$content = $this->printPhpAttributeItems($items);

$joinTableAttributeContent = $this->printAttributeContent($content);
$joinTableAttributeContent = $this->printPhpAttributeContent($content);

foreach ($this->joinColumns as $joinColumn) {
$joinTableAttributeContent .= PHP_EOL . $joinColumn->toAttributeString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,7 @@ public function getShortName(): string

public function toAttributeString(): string
{
$items = $this->createAttributeItems();
$items = $this->filterOutMissingItems($items);

$content = $this->printPhpAttributeItems($items);
return $this->printAttributeContent($content);
return $this->printItemsToAttributeString($this->createAttributeItems());
}

private function createAttributeItems(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ public function getShortName(): string

public function toAttributeString(): string
{
$items = $this->filterOutMissingItems($this->items);
$items = $this->completeItemsQuotes($items);

$content = $this->printPhpAttributeItemsAsArray($items);

return $this->printAttributeContent($content);
return $this->printItemsToAttributeAsArrayString($this->items);
}

public function getSilentKey(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public function toAttributeString(): string

$content = $this->printPhpAttributeItemsAsArray($items);

return $this->printAttributeContent($content);
return $this->printPhpAttributeContent($content);
}
}
16 changes: 0 additions & 16 deletions packages/better-php-doc-parser/src/Type/PreSlashStringType.php

This file was deleted.

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

declare(strict_types=1);

namespace Rector\NodeCollector\NodeAnalyzer;

use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\String_;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;

final class ArrayCallableClassMethodReferenceAnalyzer
{
/**
* @var NodeNameResolver
*/
private $nodeNameResolver;

public function __construct(NodeNameResolver $nodeNameResolver)
{
$this->nodeNameResolver = $nodeNameResolver;
}

/**
* Matches array like: "[$this, 'methodName']" → ['ClassName', 'methodName']
* @return string[]|null
*/
public function match(Array_ $array): ?array
{
if (count($array->items) !== 2) {
return null;
}

if ($array->items[0] === null) {
return null;
}

if ($array->items[1] === null) {
return null;
}

// $this, self, static, FQN
if (! $this->isThisVariable($array->items[0]->value)) {
return null;
}

if (! $array->items[1]->value instanceof String_) {
return null;
}

/** @var String_ $string */
$string = $array->items[1]->value;

$methodName = $string->value;
$className = $array->getAttribute(AttributeKey::CLASS_NAME);

if ($className === null) {
return null;
}

return [$className, $methodName];
}

private function isThisVariable(Node $node): bool
{
// $this
if ($node instanceof Variable && $this->nodeNameResolver->isName($node, 'this')) {
return true;
}

if ($node instanceof ClassConstFetch) {
if (! $this->nodeNameResolver->isName($node->name, 'class')) {
return false;
}

// self::class, static::class
if ($this->nodeNameResolver->isNames($node->class, ['self', 'static'])) {
return true;
}

/** @var string|null $className */
$className = $node->getAttribute(AttributeKey::CLASS_NAME);

if ($className === null) {
return false;
}

return $this->nodeNameResolver->isName($node->class, $className);
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@

use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\UnionType;
use Rector\NodeCollector\NodeAnalyzer\ArrayCallableClassMethodReferenceAnalyzer;
use Rector\NodeCollector\ValueObject\ArrayCallable;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand Down Expand Up @@ -65,9 +64,17 @@ final class ParsedFunctionLikeNodeCollector
*/
private $nodeTypeResolver;

public function __construct(NodeNameResolver $nodeNameResolver)
{
/**
* @var ArrayCallableClassMethodReferenceAnalyzer
*/
private $arrayCallableClassMethodReferenceAnalyzer;

public function __construct(
NodeNameResolver $nodeNameResolver,
ArrayCallableClassMethodReferenceAnalyzer $arrayCallableClassMethodReferenceAnalyzer
) {
$this->nodeNameResolver = $nodeNameResolver;
$this->arrayCallableClassMethodReferenceAnalyzer = $arrayCallableClassMethodReferenceAnalyzer;
}

/**
Expand All @@ -88,7 +95,7 @@ public function collect(Node $node): void

// array callable - [$this, 'someCall']
if ($node instanceof Array_) {
$arrayCallableClassAndMethod = $this->matchArrayCallableClassAndMethod($node);
$arrayCallableClassAndMethod = $this->arrayCallableClassMethodReferenceAnalyzer->match($node);
if ($arrayCallableClassAndMethod === null) {
return;
}
Expand Down Expand Up @@ -172,47 +179,6 @@ private function addMethod(ClassMethod $classMethod): void
$this->methodsByType[$className][$methodName] = $classMethod;
}

/**
* @todo decouple to NodeAnalyzer
* Matches array like: "[$this, 'methodName']" → ['ClassName', 'methodName']
* @return string[]|null
*/
private function matchArrayCallableClassAndMethod(Array_ $array): ?array
{
if (count($array->items) !== 2) {
return null;
}

if ($array->items[0] === null) {
return null;
}

// $this, self, static, FQN
if (! $this->isThisVariable($array->items[0]->value)) {
return null;
}

if ($array->items[1] === null) {
return null;
}

if (! $array->items[1]->value instanceof String_) {
return null;
}

/** @var String_ $string */
$string = $array->items[1]->value;

$methodName = $string->value;
$className = $array->getAttribute(AttributeKey::CLASS_NAME);

if ($className === null) {
return null;
}

return [$className, $methodName];
}

/**
* @param MethodCall|StaticCall $node
*/
Expand All @@ -238,36 +204,6 @@ private function addCall(Node $node): void
$this->addCallByType($node, $classType, $methodName);
}

private function isThisVariable(Node $node): bool
{
// $this
if ($node instanceof Variable && $this->nodeNameResolver->isName($node, 'this')) {
return true;
}

if ($node instanceof ClassConstFetch) {
if (! $this->nodeNameResolver->isName($node->name, 'class')) {
return false;
}

// self::class, static::class
if ($this->nodeNameResolver->isNames($node->class, ['self', 'static'])) {
return true;
}

/** @var string|null $className */
$className = $node->getAttribute(AttributeKey::CLASS_NAME);

if ($className === null) {
return false;
}

return $this->nodeNameResolver->isName($node->class, $className);
}

return false;
}

private function resolveNodeClassTypes(Node $node): Type
{
if ($node instanceof MethodCall && $node->var instanceof Variable && $node->var->name === 'this') {
Expand Down
1 change: 0 additions & 1 deletion packages/node-type-resolver/src/Node/AttributeKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ final class AttributeKey
public const CLASS_SHORT_NAME = 'classShortName';

/**
* @todo split Class node, interface node and trait node, to be compatible with other SpecificNode|null, values
* @var string
*/
public const CLASS_NODE = ClassLike::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function resolve(Node $node): Type
return $this->resolveFromFunctionDocBlock($node);
}

private function resolveFromType(Node $node)
private function resolveFromType(Node $node): Type
{
if ($node->type !== null && ! $node->type instanceof Identifier) {
$resolveTypeName = $this->nodeNameResolver->getName($node->type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public function convertNode(Node $node): ?Node

// 0. has 0 nodes, nothing to change
/** @var PhpAttributableTagNodeInterface[]&PhpDocTagValueNode[] $phpAttributableTagNodes */

$phpAttributableTagNodes = $phpDocInfo->findAllByType(PhpAttributableTagNodeInterface::class);
if ($phpAttributableTagNodes === []) {
return null;
Expand Down
Loading