Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1120 from Symplify/token-wrapper-fqn
Browse files Browse the repository at this point in the history
[TokenRunner] Add getClassTypes() to ClassWrapper + getClassName() now returns FQN
  • Loading branch information
TomasVotruba committed Sep 19, 2018
2 parents 1422ab6 + 62f2a1d commit 6850ef3
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 32 deletions.
60 changes: 42 additions & 18 deletions packages/TokenRunner/src/Wrapper/FixerWrapper/ClassWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Symplify\TokenRunner\Analyzer\FixerAnalyzer\DocBlockFinder;
use Symplify\TokenRunner\Naming\Name\NameFactory;
use function Safe\class_implements;
use function Safe\class_parents;

final class ClassWrapper
{
Expand Down Expand Up @@ -96,6 +97,11 @@ final class ClassWrapper
*/
private $methodElements = [];

/**
* @var string[]
*/
private $classTypes = [];

public function __construct(
Tokens $tokens,
int $startIndex,
Expand Down Expand Up @@ -125,24 +131,8 @@ public function getClassName(): ?string
return null;
}

$nameToken = $this->tokens[$this->getNamePosition()];

return $nameToken->getContent();
}

public function getNamePosition(): ?int
{
if ((new TokensAnalyzer($this->tokens))->isAnonymousClass($this->startIndex)) {
return null;
}

$stringTokens = $this->tokens->findGivenKind(T_STRING, $this->startIndex);
if (! count($stringTokens)) {
return null;
}
reset($stringTokens);

return (int) key($stringTokens);
$className = $this->nameFactory->createFromTokensAndStart($this->tokens, $this->getNamePosition());
return $className->getName();
}

public function getParentClassName(): ?string
Expand Down Expand Up @@ -337,6 +327,40 @@ public function getMethodElements(): array
return $this->methodElements = $methodElements;
}

/**
* @return string[]
*/
public function getClassTypes(): array
{
if ($this->classTypes) {
return $this->classTypes;
}

$classTypes = array_merge(
[$this->getClassName()],
class_parents($this->getClassName()),
class_implements($this->getClassName())
);

// unique + reindex from 0
return $this->classTypes = array_values(array_unique($classTypes));
}

private function getNamePosition(): ?int
{
if ((new TokensAnalyzer($this->tokens))->isAnonymousClass($this->startIndex)) {
return null;
}

$stringTokens = $this->tokens->findGivenKind(T_STRING, $this->startIndex);
if (! count($stringTokens)) {
return null;
}
reset($stringTokens);

return (int) key($stringTokens);
}

/**
* @return mixed[]
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

use PhpCsFixer\Tokenizer\Tokens;
use Symplify\TokenRunner\Tests\AbstractContainerAwareTestCase;
use Symplify\TokenRunner\Tests\Wrapper\FixerWrapper\ClassWrapper\Source\AbstractClass;
use Symplify\TokenRunner\Tests\Wrapper\FixerWrapper\ClassWrapper\Source\SomeClass;
use Symplify\TokenRunner\Tests\Wrapper\FixerWrapper\ClassWrapper\Source\SomeInterface;
use Symplify\TokenRunner\Wrapper\FixerWrapper\ClassWrapper;
use Symplify\TokenRunner\Wrapper\FixerWrapper\ClassWrapperFactory;
use function Safe\file_get_contents;

Expand All @@ -14,19 +18,37 @@ final class ClassWrapperTest extends AbstractContainerAwareTestCase
*/
private $classWrapperFactory;

/**
* @var ClassWrapper
*/
private $classWrapper;

protected function setUp(): void
{
$this->classWrapperFactory = $this->container->get(ClassWrapperFactory::class);
}

public function testGet(): void
{
$tokens = Tokens::fromCode(file_get_contents(__DIR__ . '/Source/AbstractSomeClass.php'));
$tokens = Tokens::fromCode(file_get_contents(__DIR__ . '/Source/SomeClass.php'));
$classTokens = $tokens->findGivenKind([T_CLASS], 0);

$classTokenPosition = key(array_pop($classTokens));
$classWrapper = $this->classWrapperFactory->createFromTokensArrayStartPosition($tokens, $classTokenPosition);

$this->assertSame('Rector\Rector\AbstractRector', $classWrapper->getParentClassName());
$this->classWrapper = $this->classWrapperFactory->createFromTokensArrayStartPosition(
$tokens,
$classTokenPosition
);
}

public function testGetNames(): void
{
$this->assertSame(SomeClass::class, $this->classWrapper->getClassName());
$this->assertSame(AbstractClass::class, $this->classWrapper->getParentClassName());
}

public function testGetClassTypes(): void
{
$this->assertSame([
SomeClass::class,
AbstractClass::class,
SomeInterface::class,
], $this->classWrapper->getClassTypes());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php declare(strict_types=1);

namespace Symplify\TokenRunner\Tests\Wrapper\FixerWrapper\ClassWrapper\Source;

abstract class AbstractClass implements SomeInterface
{
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php declare(strict_types=1);

namespace Symplify\TokenRunner\Tests\Wrapper\FixerWrapper\ClassWrapper\Source;

final class SomeClass extends AbstractClass
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php declare(strict_types=1);

namespace Symplify\TokenRunner\Tests\Wrapper\FixerWrapper\ClassWrapper\Source;

interface SomeInterface
{
}

0 comments on commit 6850ef3

Please sign in to comment.