[scoper] unprefix string classes to match types (#5025)

This commit is contained in:
Tomas Votruba 2020-12-28 23:09:11 +01:00 committed by GitHub
parent acdc7ee6ee
commit 52e604c6dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 81 additions and 21 deletions

View File

@ -5,6 +5,7 @@ on:
push:
branches:
- master
pull_request: null
jobs:
build_scoped_rector:

View File

@ -29,6 +29,7 @@ note "Starts"
note "Coping root files to $NESTED_DIRECTORY directory"
rsync -av * "$NESTED_DIRECTORY" --quiet
note "Running composer update without dev"
composer update --no-dev --no-progress --ansi --working-dir "$NESTED_DIRECTORY"
@ -43,14 +44,15 @@ composer config platform-check false
note "Running scoper to $SCOPED_DIRECTORY"
wget https://github.com/humbug/php-scoper/releases/download/0.14.0/php-scoper.phar -N --no-verbose
php php-scoper.phar add-prefix bin config packages rules src templates vendor composer.json --output-dir "../$SCOPED_DIRECTORY" --config scoper.php.inc --force --ansi --working-dir "$NESTED_DIRECTORY"
php php-scoper.phar add-prefix bin config packages rules src templates vendor composer.json --output-dir "../$SCOPED_DIRECTORY" --config scoper.php --force --ansi --working-dir "$NESTED_DIRECTORY"
note "Dumping Composer Autoload"
composer dump-autoload --working-dir "$SCOPED_DIRECTORY" --ansi --optimize --classmap-authoritative --no-dev
# clean up
rm -rf "$NESTED_DIRECTORY"
# copy metafiles needed for release
note "Copy metafiles like composer.json, .github etc to repository"
rm -f "$SCOPED_DIRECTORY/composer.json"
@ -60,5 +62,4 @@ cp -R scoped/. "$SCOPED_DIRECTORY"
chmod 777 "$SCOPED_DIRECTORY/bin/rector"
chmod 777 "$SCOPED_DIRECTORY/bin/rector.php"
note "Finished"

View File

@ -30,15 +30,15 @@
"symfony/dependency-injection": "^4.4.8|^5.1",
"symfony/finder": "^4.4.8|^5.1",
"symfony/http-kernel": "^4.4.8|^5.1",
"symplify/autowire-array-parameter": "^9.0.17",
"symplify/console-color-diff": "^9.0.17",
"symplify/package-builder": "^9.0.17",
"symplify/rule-doc-generator": "^9.0.17",
"symplify/set-config-resolver": "^9.0.17",
"symplify/simple-php-doc-parser": "^9.0.17",
"symplify/skipper": "^9.0.17",
"symplify/smart-file-system": "^9.0.17",
"symplify/symfony-php-config": "^9.0.17",
"symplify/autowire-array-parameter": "^9.0.21",
"symplify/console-color-diff": "^9.0.21",
"symplify/package-builder": "^9.0.21",
"symplify/rule-doc-generator": "^9.0.21",
"symplify/set-config-resolver": "^9.0.21",
"symplify/simple-php-doc-parser": "^9.0.21",
"symplify/skipper": "^9.0.21",
"symplify/smart-file-system": "^9.0.21",
"symplify/symfony-php-config": "^9.0.21",
"webmozart/assert": "^1.9"
},
"require-dev": {
@ -51,12 +51,12 @@
"phpstan/phpstan-nette": "^0.12.12",
"phpunit/phpunit": "^9.5",
"sebastian/diff": "^4.0.4",
"symplify/changelog-linker": "^9.0.17",
"symplify/coding-standard": "^9.0.17",
"symplify/easy-coding-standard": "^9.0.17",
"symplify/easy-testing": "^9.0.17",
"symplify/phpstan-extensions": "^9.0.17",
"symplify/phpstan-rules": "^9.0.17",
"symplify/changelog-linker": "^9.0.21",
"symplify/coding-standard": "^9.0.21",
"symplify/easy-coding-standard": "^9.0.21",
"symplify/easy-testing": "^9.0.21",
"symplify/phpstan-extensions": "^9.0.21",
"symplify/phpstan-rules": "^9.0.21",
"tracy/tracy": "^2.7"
},
"replace": {

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
use Nette\Utils\DateTime;
use Nette\Utils\Strings;
use Rector\Compiler\PhpScoper\StaticEasyPrefixer;
use Rector\Compiler\Unprefixer;
use Rector\Compiler\ValueObject\ScoperOption;
require_once __DIR__ . '/vendor/autoload.php';
@ -28,8 +29,30 @@ return [
ScoperOption::PATCHERS => [
// [BEWARE] $filePath is absolute!
// unprefix string classes, as they're string on purpose - they have to be checked in original form, not prefixed
function (string $filePath, string $prefix, string $content): string {
// skip vendor
if (Strings::contains($filePath, 'vendor/')) {
return $content;
}
// skip bin/rector.php for composer autoload class
if (Strings::endsWith($filePath, 'bin/rector.php')) {
return $content;
}
// skip scoper-autoload
if (Strings::endsWith($filePath, 'vendor/scoper-autoload.php')) {
return $content;
}
return Unprefixer::unprefixQuoted($content, $prefix);
},
// scoper missed PSR-4 autodiscovery in Symfony
function (string $filePath, string $prefix, string $content): string {
// scoper missed PSR-4 autodiscovery in Symfony
if (! Strings::endsWith($filePath, 'config.php') && ! Strings::endsWith($filePath, 'services.php')) {
return $content;

View File

@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace Rector\Compiler;
use Nette\Utils\Strings;
final class Unprefixer
{
/**
* @var string
* @see https://regex101.com/r/P8sXfr/1
*/
private const QUOTED_VALUE_REGEX = '#\'\\\\(\w|@)#';
public static function unprefixQuoted(string $content, string $prefix): string
{
$match = sprintf('\'%s\\\\r\\\\n\'', $prefix);
$content = Strings::replace($content, '#' . $match . '#', '\'\\\\r\\\\n\'');
$match = sprintf('\'%s\\\\', $prefix);
$content = Strings::replace($content, '#' . $match . '#', "'");
$match = sprintf('\'%s\\\\\\\\', $prefix);
$content = Strings::replace($content, '#' . $match . '#', "'");
return self::unPreSlashQuotedValues($content);
}
private static function unPreSlashQuotedValues(string $content): string
{
return Strings::replace($content, self::QUOTED_VALUE_REGEX, "'$1");
}
}

View File

@ -9,7 +9,7 @@ use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\Expression;
use PhpParser\NodeFinder;
use Symplify\PHPStanRules\Naming\SimpleNameResolver;
use Symplify\Astral\Naming\SimpleNameResolver;
use Symplify\PHPStanRules\ValueObject\PHPStanAttributeKey;
final class SymfonyConfigRectorValueObjectResolver

View File

@ -6,7 +6,7 @@ namespace Rector\PHPStanExtensions\NodeAnalyzer;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Type\TypeWithClassName;
use Symplify\PHPStanRules\Naming\SimpleNameResolver;
use Symplify\Astral\Naming\SimpleNameResolver;
final class TypeAndNameAnalyzer
{

View File

@ -12,7 +12,7 @@ use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use Rector\PHPStanExtensions\NodeAnalyzer\SymfonyConfigRectorValueObjectResolver;
use Rector\PHPStanExtensions\NodeAnalyzer\TypeAndNameAnalyzer;
use Symplify\PHPStanRules\Naming\SimpleNameResolver;
use Symplify\Astral\Naming\SimpleNameResolver;
/**
* @see \Rector\PHPStanExtensions\Tests\Rule\RectorRuleAndValueObjectHaveSameStartsRule\RectorRuleAndValueObjectHaveSameStartsRuleTest