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 @@ -24,7 +24,7 @@
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\Core\Reflection\ClassMethodReflectionHelper;
use Rector\Core\Reflection\FunctionReflectionHelper;
use Rector\Core\Reflection\FunctionAnnotationResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use ReflectionFunction;

Expand All @@ -39,9 +39,9 @@ final class AnnotateThrowablesRector extends AbstractRector
private $throwablesToAnnotate = [];

/**
* @var FunctionReflectionHelper
* @var FunctionAnnotationResolver
*/
private $functionReflectionHelper;
private $functionAnnotationResolver;

/**
* @var ClassMethodReflectionHelper
Expand All @@ -56,9 +56,9 @@ final class AnnotateThrowablesRector extends AbstractRector
public function __construct(
ClassMethodReflectionHelper $classMethodReflectionHelper,
ClassResolver $classResolver,
FunctionReflectionHelper $functionReflectionHelper
FunctionAnnotationResolver $functionAnnotationResolver
) {
$this->functionReflectionHelper = $functionReflectionHelper;
$this->functionAnnotationResolver = $functionAnnotationResolver;
$this->classMethodReflectionHelper = $classMethodReflectionHelper;
$this->classResolver = $classResolver;
}
Expand Down Expand Up @@ -197,7 +197,7 @@ private function analyzeStmtFuncCall(FuncCall $funcCall): int
}

$reflectedFunction = new ReflectionFunction($functionFqn);
$foundThrownThrowables = $this->functionReflectionHelper->extractFunctionAnnotatedThrows($reflectedFunction);
$foundThrownThrowables = $this->functionAnnotationResolver->extractFunctionAnnotatedThrows($reflectedFunction);
$alreadyAnnotatedThrowables = $this->extractAlreadyAnnotatedThrowables($funcCall);
return $this->diffThrowables($foundThrownThrowables, $alreadyAnnotatedThrowables);
}
Expand Down
4 changes: 4 additions & 0 deletions src/PhpDoc/PhpDocTagsFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Nette\Utils\Strings;

/**
* @see \Rector\Core\Tests\PhpDoc\PhpDocTagsFinderTest
*/
final class PhpDocTagsFinder
{
/**
Expand All @@ -31,6 +34,7 @@ public function extractTagsFromStringedDocblock(string $dockblock, string $tagNa
// This is required as @return, for example, can be written as "@return ClassOne|ClassTwo|ClassThree"
return explode('|', str_replace($tagName . ' ', '', $matchingTag));
};

$matchingTags = array_map($explode, $matchingTags);

return array_merge(...$matchingTags);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Rector\Core\PhpParser\Parser\FunctionParser;
use ReflectionFunction;

final class FunctionReflectionHelper
final class FunctionAnnotationResolver
{
/**
* @var ClassNaming
Expand Down
34 changes: 34 additions & 0 deletions tests/PhpDoc/PhpDocTagsFinderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\PhpDoc;

use Nette\Utils\FileSystem;
use Rector\Core\HttpKernel\RectorKernel;
use Rector\Core\PhpDoc\PhpDocTagsFinder;
use Symplify\PackageBuilder\Tests\AbstractKernelTestCase;

final class PhpDocTagsFinderTest extends AbstractKernelTestCase
{
/**
* @var PhpDocTagsFinder
*/
private $phpDocTagsFinder;

protected function setUp(): void
{
$this->bootKernel(RectorKernel::class);
$this->phpDocTagsFinder = self::$container->get(PhpDocTagsFinder::class);
}

public function test(): void
{
$docContent = FileSystem::read(__DIR__ . '/Source/doc_block_throws.txt');

$throwsTags = $this->phpDocTagsFinder->extractTagsFromStringedDocblock($docContent, 'throws');

$this->assertCount(3, $throwsTags);
$this->assertSame(['A', 'B', 'C'], $throwsTags);
}
}
3 changes: 3 additions & 0 deletions tests/PhpDoc/Source/doc_block_throws.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/**
* @throws A|B|C
*/
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
<?php

function unsetAndIsset()
namespace Rector\Core\Tests\Rector\MagicDisclosure\UnsetAndIssetToMethodCallRector\Fixture;

class SomeClass
{
$container = new Nette\DI\Container;
isset($container['someService']);
unset($container['someService']);
function unsetAndIsset()
{
$container = new \Rector\Core\Tests\Rector\MagicDisclosure\UnsetAndIssetToMethodCallRector\Source\LocalContainer;
isset($container['someService']);
unset($container['someService']);
}
}

?>
-----
<?php

function unsetAndIsset()
namespace Rector\Core\Tests\Rector\MagicDisclosure\UnsetAndIssetToMethodCallRector\Fixture;

class SomeClass
{
$container = new Nette\DI\Container;
$container->hasService('someService');
$container->removeService('someService');
function unsetAndIsset()
{
$container = new \Rector\Core\Tests\Rector\MagicDisclosure\UnsetAndIssetToMethodCallRector\Source\LocalContainer;
$container->hasService('someService');
$container->removeService('someService');
}
}

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

declare(strict_types=1);


namespace Rector\Core\Tests\Rector\MagicDisclosure\UnsetAndIssetToMethodCallRector\Source;

final class LocalContainer
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace Rector\Core\Tests\Rector\MagicDisclosure\UnsetAndIssetToMethodCallRector;

use Iterator;
use Nette\DI\Container;
use Rector\Core\Rector\MagicDisclosure\UnsetAndIssetToMethodCallRector;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\Core\Tests\Rector\MagicDisclosure\UnsetAndIssetToMethodCallRector\Source\LocalContainer;

final class UnsetAndIssetToMethodCallRectorTest extends AbstractRectorTestCase
{
Expand All @@ -32,7 +32,7 @@ protected function getRectorsWithConfiguration(): array
return [
UnsetAndIssetToMethodCallRector::class => [
'$typeToMethodCalls' => [
Container::class => [
LocalContainer::class => [
'isset' => 'hasService',
'unset' => 'removeService',
],
Expand Down