mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-17 13:28:18 +01:00
add ScopingTest
This commit is contained in:
parent
cfc32ab498
commit
d4777e9bf3
@ -7,8 +7,9 @@ declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use Isolated\Symfony\Component\Finder\Finder;
|
||||
use Nette\Neon\Neon;
|
||||
use Nette\Utils\Strings;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
|
||||
final class WhitelistedStubsProvider
|
||||
{
|
||||
@ -48,26 +49,21 @@ final class EasyPrefixer
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
public const ALLOWED_PREFIXES = ['Hoa\*', 'PhpParser\*', 'PHPStan\*', 'Rector\*'];
|
||||
public const EXCLUDED_NAMESPACES = ['Hoa\*', 'PhpParser\*', 'PHPStan\*', 'Rector\*'];
|
||||
|
||||
public static function prefixClass(string $class, string $prefix): string
|
||||
{
|
||||
// @todo move to allowed prefixes
|
||||
if (strpos($class, 'PHPStan\\') === 0) {
|
||||
return $class;
|
||||
}
|
||||
if (strpos($class, 'PhpParser\\') === 0) {
|
||||
return $class;
|
||||
}
|
||||
if (strpos($class, 'Rector\\') === 0) {
|
||||
return $class;
|
||||
}
|
||||
if (strpos($class, 'Hoa\\') === 0) {
|
||||
return $class;
|
||||
foreach (self::EXCLUDED_NAMESPACES as $excludedNamespace) {
|
||||
$excludedNamespace = Strings::substring($excludedNamespace, 0, -2) . '\\';
|
||||
if (Strings::startsWith($class, $excludedNamespace)) {
|
||||
return $class;
|
||||
}
|
||||
}
|
||||
|
||||
if (strpos($class, '@') === 0) {
|
||||
return $class;
|
||||
}
|
||||
|
||||
return $prefix . '\\' . $class;
|
||||
}
|
||||
}
|
||||
@ -81,12 +77,14 @@ return [
|
||||
if ($filePath !== 'bin/rector') {
|
||||
return $content;
|
||||
}
|
||||
|
||||
return str_replace('__DIR__ . \'/..', '\'phar://rector.phar', $content);
|
||||
},
|
||||
function (string $filePath, string $prefix, string $content): string {
|
||||
if ($filePath !== 'vendor/nette/di/src/DI/Compiler.php') {
|
||||
return $content;
|
||||
}
|
||||
|
||||
return str_replace(
|
||||
'|Nette\\\\DI\\\\Statement',
|
||||
sprintf('|\\\\%s\\\\Nette\\\\DI\\\\Statement', $prefix),
|
||||
@ -108,6 +106,7 @@ return [
|
||||
if ($filePath !== 'vendor/nette/di/src/DI/Extensions/ExtensionsExtension.php') {
|
||||
return $content;
|
||||
}
|
||||
|
||||
$content = str_replace(sprintf('\'%s\\\\string', $prefix), '\'string', $content);
|
||||
return str_replace(
|
||||
'|Nette\\\\DI\\\\Definitions\\\\Statement',
|
||||
@ -119,6 +118,7 @@ return [
|
||||
if ($filePath !== 'src/Testing/TestCase.php') {
|
||||
return $content;
|
||||
}
|
||||
|
||||
return str_replace(
|
||||
sprintf('\\%s\\PHPUnit\\Framework\\TestCase', $prefix),
|
||||
'\\PHPUnit\\Framework\\TestCase',
|
||||
@ -129,6 +129,7 @@ return [
|
||||
if ($filePath !== 'src/Testing/LevelsTestCase.php') {
|
||||
return $content;
|
||||
}
|
||||
|
||||
return str_replace(
|
||||
[
|
||||
sprintf('\\%s\\PHPUnit\\Framework\\AssertionFailedError', $prefix),
|
||||
@ -141,15 +142,18 @@ return [
|
||||
|
||||
function (string $filePath, string $prefix, string $content): string {
|
||||
// only *.yaml files
|
||||
if (strpos($filePath, '.yaml') === false) {
|
||||
if (! Strings::endsWith($filePath, '.yaml')) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
// @see https://github.com/rectorphp/rector/issues/3227
|
||||
if (strpos($filePath, 'config/set/') !== 0) {
|
||||
if (Strings::startsWith($filePath, 'config/set/')) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
var_dump($content);
|
||||
die;
|
||||
|
||||
// @todo - prefix classes in yaml files?
|
||||
return $content;
|
||||
},
|
||||
@ -157,7 +161,7 @@ return [
|
||||
// mimics https://github.com/phpstan/phpstan-src/commit/5a6a22e5c4d38402c8cc888d8732360941c33d43#diff-463a36e4a5687fb2366b5ee56cdad92d
|
||||
function (string $filePath, string $prefix, string $content): string {
|
||||
// only *.neon files
|
||||
if (strpos($filePath, '.neon') === false) {
|
||||
if (! Strings::endsWith($filePath, '.neon')) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
@ -188,7 +192,10 @@ return [
|
||||
}
|
||||
}
|
||||
|
||||
return Neon::encode($updatedNeon, Neon::BLOCK);
|
||||
$updatedContent = Neon::encode($updatedNeon, Neon::BLOCK);
|
||||
|
||||
// default indent is tab, we have spaces
|
||||
return Strings::replace($updatedContent, '#\t#', ' ');
|
||||
},
|
||||
|
||||
// mimics https://github.com/phpstan/phpstan-src/commit/fd8f0a852207a1724ae4a262f47d9a449de70da4#diff-463a36e4a5687fb2366b5ee56cdad92d
|
||||
@ -200,5 +207,5 @@ return [
|
||||
return str_replace(sprintf('\'%s\\\\', $prefix), '\'', $content);
|
||||
},
|
||||
],
|
||||
'whitelist' => EasyPrefixer::ALLOWED_PREFIXES,
|
||||
'whitelist' => EasyPrefixer::EXCLUDED_NAMESPACES,
|
||||
];
|
||||
|
@ -0,0 +1,9 @@
|
||||
services:
|
||||
Rector\Core\Rector\Function_\FunctionToStaticCallRector:
|
||||
$functionToStaticCall:
|
||||
file_get_contents: ['Nette\Utils\FileSystem', 'read']
|
||||
-----
|
||||
services:
|
||||
Rector\Core\Rector\Function_\FunctionToStaticCallRector:
|
||||
$functionToStaticCall:
|
||||
file_get_contents: ['Nette\Utils\FileSystem', 'read']
|
7
compiler/tests/Fixture/one_service_class.neon
Normal file
7
compiler/tests/Fixture/one_service_class.neon
Normal file
@ -0,0 +1,7 @@
|
||||
services:
|
||||
-
|
||||
class: SomeService
|
||||
-----
|
||||
services:
|
||||
-
|
||||
class: Prefix__\SomeService
|
63
compiler/tests/ScopingTest.php
Normal file
63
compiler/tests/ScopingTest.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Compiler\Tests;
|
||||
|
||||
use Iterator;
|
||||
use Nette\Utils\Strings;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class ScopingTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private const PREFIX = 'Prefix__';
|
||||
|
||||
/**
|
||||
* @var callable[]
|
||||
*/
|
||||
private $patcherCallbacks = [];
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$scoper = require __DIR__ . '/../build/scoper.inc.php';
|
||||
$this->patcherCallbacks = $scoper['patchers'];
|
||||
}
|
||||
|
||||
public function test(): void
|
||||
{
|
||||
/** @var SmartFileInfo[] $fileInfos */
|
||||
$fileInfos = self::loadFromDirectory(__DIR__ . '/Fixture/');
|
||||
|
||||
foreach ($fileInfos as $fileInfo) {
|
||||
[$content, $expectedContent] = Strings::split($fileInfo->getContents(), "#-----\n#");
|
||||
|
||||
foreach ($this->patcherCallbacks as $patcherCallback) {
|
||||
$relativeFilePath = $fileInfo->getRelativeFilePathFromDirectory(__DIR__ . '/Fixture');
|
||||
$content = $patcherCallback($relativeFilePath, self::PREFIX, $content);
|
||||
}
|
||||
|
||||
// normalize end-line spaces
|
||||
$expectedContent = rtrim($expectedContent);
|
||||
$content = rtrim($content);
|
||||
$this->assertSame($expectedContent, $content);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<SmartFileInfo>
|
||||
*/
|
||||
private static function loadFromDirectory(string $directory): Iterator
|
||||
{
|
||||
$finder = (new Finder())->files()
|
||||
->in($directory);
|
||||
|
||||
foreach ($finder as $fileInfo) {
|
||||
yield new SmartFileInfo($fileInfo->getRealPath());
|
||||
}
|
||||
}
|
||||
}
|
@ -203,7 +203,8 @@
|
||||
"Rector\\Performance\\Tests\\": "rules/performance/tests",
|
||||
"Rector\\Naming\\Tests\\": "rules/naming/tests",
|
||||
"Rector\\Order\\Tests\\": "rules/order/tests",
|
||||
"Rector\\MockistaToMockery\\Tests\\": "rules/mockista-to-mockery/tests"
|
||||
"Rector\\MockistaToMockery\\Tests\\": "rules/mockista-to-mockery/tests",
|
||||
"Rector\\Compiler\\Tests\\": "compiler/tests"
|
||||
},
|
||||
"classmap": [
|
||||
"rules/cakephp/tests/Rector/Name/ImplicitShortClassNameUseStatementRector/Source",
|
||||
|
Loading…
x
Reference in New Issue
Block a user