Skip to content

Commit

Permalink
[scoped] Build with Scoper 0.17.2 (#2445)
Browse files Browse the repository at this point in the history
* remove ScoperOption, not helpful

* try Scoper 0.17.2

* use dedicated repository

* expose helper functions

* add exclude ns

* expose normalizer

* try to fix import

* container ocnfigurator

* unpreifx file info for real

* expose container configuraotr

* deploy to original repository

* complete polyfill list

* expose rector running constant

* slash

* skip RuleDefinition explicitly
  • Loading branch information
TomasVotruba committed Jun 7, 2022
1 parent 39c7ae6 commit d4b7b48
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 212 deletions.
20 changes: 5 additions & 15 deletions .github/workflows/build_scoped_rector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
name: Build Scoped Rector

on:
# testing
pull_request: null
push:
branches:
- main
Expand Down Expand Up @@ -53,8 +55,7 @@ jobs:

# early downgrade individual functions
- run: |
bin/rector process src/functions -c build/config/config-downgrade.php --ansi
bin/rector process vendor/symfony/string/Resources/functions.php -c build/config/config-downgrade.php --ansi
bin/rector process src/functions vendor/symfony/string/Resources/functions.php -c build/config/config-downgrade.php --ansi
# 1. copy files to $NESTED_DIRECTORY directory Exclude the scoped/nested directories to prevent rsync from copying in a loop
- run: rsync --exclude rector-build -av * rector-build --quiet
Expand All @@ -64,17 +65,6 @@ jobs:
# 2. downgrade rector
- run: php -d memory_limit=-1 bin/rector process rector-build/bin rector-build/config rector-build/src rector-build/packages rector-build/rules rector-build/vendor --config build/config/config-downgrade.php --ansi --no-diffs

# scoped using php-scoper.phar which require #[\ReturnTypeWillChange] inside so use php 8.0 for scoping
-
uses: shivammathur/setup-php@v2
with:
php-version: 8.0
coverage: none

# fixes https://github.com/rectorphp/rector/pull/4559/checks?check_run_id=1359814403, see https://github.com/shivammathur/setup-php#composer-github-oauth
env:
COMPOSER_TOKEN: ${{ secrets.ACCESS_TOKEN }}

# 3. prefix classes
- run: sh build/build-rector-scoped.sh rector-build rector-prefixed-downgraded

Expand All @@ -84,8 +74,8 @@ jobs:
with:
php-version: 7.2
coverage: none
- run: composer global require php-parallel-lint/php-parallel-lint
- run: /home/runner/.composer/vendor/bin/parallel-lint rector-prefixed-downgraded --exclude rector-prefixed-downgraded/stubs --exclude rector-prefixed-downgraded/vendor/tracy/tracy/examples --exclude rector-prefixed-downgraded/vendor/rector/rector-generator/templates --exclude rector-prefixed-downgraded/vendor/symfony/contracts/Cache --exclude rector-prefixed-downgraded/vendor/symfony/contracts/HttpClient/Test
- run: composer global require php-parallel-lint/php-parallel-lint --ansi
- run: /home/runner/.composer/vendor/bin/parallel-lint rector-prefixed-downgraded --exclude rector-prefixed-downgraded/stubs --exclude rector-prefixed-downgraded/vendor/tracy/tracy/examples --exclude rector-prefixed-downgraded/vendor/ssch/typo3-rector/templates/maker --exclude rector-prefixed-downgraded/vendor/rector/rector-generator/templates --exclude rector-prefixed-downgraded/vendor/symfony/contracts/Cache --exclude rector-prefixed-downgraded/vendor/symfony/contracts/HttpClient/Test

# 5. copy repository meta files
- run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/php_linter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ jobs:
php-version: 8.1
coverage: none

- run: composer global require php-parallel-lint/php-parallel-lint
- run: composer global require php-parallel-lint/php-parallel-lint --ansi

- run: /home/runner/.composer/vendor/bin/parallel-lint src bin/rector config tests packages rules --colors --exclude rules/psr4/tests/Rector/Namespace_/MultipleClassFileToPsr4ClassesRector/Source --exclude packages/node-type-resolver/tests/PerNodeTypeResolver/PropertyFetchTypeResolver/Source --exclude rules/nette-kdyby/tests/Rector/MethodCall/ReplaceEventManagerWithEventSubscriberRector/Source/ExpectedSomeClassCopyEvent.php --exclude rules/nette-kdyby/tests/Rector/MethodCall/ReplaceMagicPropertyEventWithEventClassRector/Source --exclude rules/type-declaration/tests/Rector/ClassMethod/ParamTypeFromStrictTypedPropertyRector/Source
2 changes: 1 addition & 1 deletion .github/workflows/rector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
paths:
- src tests rules-tests packages packages-tests
- rules
- config utils scoper.php
- config utils

runs-on: ubuntu-latest
timeout-minutes: 10
Expand Down
6 changes: 0 additions & 6 deletions build/build-preload.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,6 @@ public function buildPreloadScript(string $buildDirectory): void
// 1. fine php-parser file infos
$fileInfos = $this->findPhpParserFiles($vendorDir);

// append ContainerConfiguration to avoid accidental load of prefixed one from another tool
$fileInfos[] = new SplFileInfo(__DIR__ . '/../vendor/symfony/dependency-injection/Loader/Configurator/AbstractConfigurator.php');
$fileInfos[] = new SplFileInfo(__DIR__ . '/../vendor/symfony/dependency-injection/Loader/Configurator/ContainerConfigurator.php');
// attempt to fix https://github.com/rectorphp/rector-src/pull/1199
$fileInfos[] = new SplFileInfo(__DIR__ . '/../vendor/symfony/contracts/Deprecation/function.php');

// 2. put first-class usages first
usort($fileInfos, function (SplFileInfo $firstFileInfo, SplFileInfo $secondFileInfo) {
$firstFilePosition = $this->matchFilePriorityPosition($firstFileInfo);
Expand Down
15 changes: 3 additions & 12 deletions build/build-rector-scoped.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ export TERM=xterm-color

# show errors
set -e

# script fails if trying to access to an undefined variable
set -u


# functions
note()
{
Expand All @@ -34,18 +31,12 @@ rm -f "$BUILD_DIRECTORY/phpstan-for-rector.neon"

# 2. scope it
note "Running scoper to $RESULT_DIRECTORY"
wget https://github.com/humbug/php-scoper/releases/download/0.14.0/php-scoper.phar -N --no-verbose
wget https://github.com/humbug/php-scoper/releases/download/0.17.2/php-scoper.phar -N --no-verbose

# Work around possible PHP memory limits
php -d memory_limit=-1 php-scoper.phar add-prefix bin config src packages rules vendor composer.json --output-dir "../$RESULT_DIRECTORY" --config scoper.php --force --ansi --working-dir "$BUILD_DIRECTORY";

if test -z ${PHP80_BIN_PATH+y}; then
php -d memory_limit=-1 php-scoper.phar add-prefix bin config src packages rules vendor composer.json --output-dir "../$RESULT_DIRECTORY" --config scoper.php --force --ansi --working-dir "$BUILD_DIRECTORY";
else
echo "scoping with specify PHP80_BIN_PATH env";
$PHP80_BIN_PATH -d memory_limit=-1 php-scoper.phar add-prefix bin config src packages rules vendor composer.json --output-dir "../$RESULT_DIRECTORY" --config scoper.php --force --ansi --working-dir "$BUILD_DIRECTORY";
fi

# note "Dumping Composer Autoload"
note "Dumping Composer Autoload"
composer dump-autoload --working-dir "$RESULT_DIRECTORY" --ansi --classmap-authoritative --no-dev

rm -rf "$BUILD_DIRECTORY"
Expand Down
2 changes: 1 addition & 1 deletion full_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ composer install --ansi
composer install --no-dev --ansi

# early downgrade individual functions
bin/rector process src/functions -c build/config/config-downgrade.php --ansi --clear-cache --no-diffs
bin/rector process src/functions -c build/config/config-downgrade.php --ansi --clear-cache
bin/rector process vendor/symfony/string/Resources/functions.php -c build/config/config-downgrade.php --ansi --clear-cache

rsync --exclude rector-build -av * rector-build --quiet
Expand Down
3 changes: 0 additions & 3 deletions preload.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,3 @@
require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Parser/Tokens.php';
require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/ParserFactory.php';
require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/PrettyPrinter/Standard.php';
require_once __DIR__ . '/vendor/symfony/dependency-injection/Loader/Configurator/AbstractConfigurator.php';
require_once __DIR__ . '/vendor/symfony/dependency-injection/Loader/Configurator/ContainerConfigurator.php';
require_once __DIR__ . '/vendor/symfony/contracts/Deprecation/function.php';
162 changes: 35 additions & 127 deletions scoper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Nette\Utils\DateTime;
use Nette\Utils\Strings;
use Rector\Compiler\PhpScoper\StaticEasyPrefixer;
use Rector\Compiler\Unprefixer;
use Rector\Core\Application\VersionResolver;

Expand All @@ -13,56 +12,51 @@
// remove phpstan, because it is already prefixed in its own scope
$dateTime = DateTime::from('now');
$timestamp = $dateTime->format('Ymd');
/**
* @var array<string, string[]>
*/
const UNPREFIX_CLASSES_BY_FILE = [
// make UT=1 in tests work
'packages/Testing/PHPUnit/AbstractRectorTestCase.php' => [
'PHPUnit\Framework\ExpectationFailedException',

// see https://github.com/humbug/php-scoper/blob/master/docs/configuration.md#configuration
return [
'prefix' => 'RectorPrefix' . $timestamp,

// exclude
'exclude-classes' => [
'PHPUnit\Framework\Constraint\IsEqual',
'PHPUnit\Framework\TestCase',
'PHPUnit\Framework\ExpectationFailedException',
// namespce with slash does not handle it
'Symplify\RuleDocGenerator\ValueObject\RuleDefinition',
],
'exclude-namespaces' => ['#^Rector#', '#^PhpParser#', '#^PHPStan#', '#^Symplify\\\\RuleDocGenerator#'],
'exclude-files' => [
'vendor/symfony/deprecation-contracts/function.php',
'vendor/symfony/polyfill-intl-normalizer/bootstrap.php',
'vendor/symfony/polyfill-intl-normalizer/bootstrap80.php',
'vendor/symfony/polyfill-mbstring/bootstrap.php',
'vendor/symfony/polyfill-mbstring/bootstrap80.php',
'vendor/symfony/polyfill-php80/bootstrap.php',
'vendor/symfony/polyfill-php80/Resources/stubs/Attribute.php',
'vendor/symfony/polyfill-php80/Resources/stubs/PhpToken.php',
'vendor/symfony/polyfill-php80/Resources/stubs/Stringable.php',
'vendor/symfony/polyfill-php80/Resources/stubs/ValueError.php',
'vendor/symfony/polyfill-php80/Resources/stubs/UnhandledMatchError.php',
],

// unprefixed ComposerJson as part of public API in ComposerRectorInterface
'rules/Composer/Contract/Rector/ComposerRectorInterface.php' => [
// expose
'expose-classes' => [
'Normalizer',
// used by public API
'Symplify\SmartFileSystem\SmartFileInfo',
'Symplify\ComposerJsonManipulator\ValueObject\ComposerJson',
'Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator',
],
'packages/Testing/PHPUnit/AbstractTestCase.php' => ['PHPUnit\Framework\TestCase'],
];
'expose-functions' => ['u', 'b', 's', 'trigger_deprecation'],
'expose-constants' => ['__RECTOR_RUNNING__'],

/**
* @see https://regex101.com/r/LMDq0p/1
* @var string
*/
const POLYFILL_FILE_NAME_REGEX = '#vendor\/symfony\/polyfill\-(.*)\/bootstrap(.*?)\.php#';

/**
* @see https://regex101.com/r/RBZ0bN/1
* @var string
*/
const POLYFILL_STUBS_NAME_REGEX = '#vendor\/symfony\/polyfill\-(.*)\/Resources\/stubs#';

// see https://github.com/humbug/php-scoper
return [
'prefix' => 'RectorPrefix' . $timestamp,
'whitelist' => StaticEasyPrefixer::getExcludedNamespacesAndClasses(),
'patchers' => [
// fix short import bug, @see https://github.com/rectorphp/rector-scoper-017/blob/23f3256a6f5a18483d6eb4659d69ba117501e2e3/vendor/nikic/php-parser/lib/PhpParser/Builder/Declaration.php#L6
function (string $filePath, string $prefix, string $content): string {
foreach (UNPREFIX_CLASSES_BY_FILE as $endFilePath => $unprefixClasses) {
if (! \str_ends_with($filePath, $endFilePath)) {
continue;
}

foreach ($unprefixClasses as $unprefixClass) {
$doubleQuotedClass = preg_quote('\\' . $unprefixClass);
$content = Strings::replace($content, '#' . $prefix . $doubleQuotedClass . '#', $unprefixClass);
}
}

return $content;
return str_replace(sprintf('use %s\PhpParser;', $prefix), 'use PhpParser;', $content);
},


function (string $filePath, string $prefix, string $content): string {
if (! \str_ends_with($filePath, 'src/Application/VersionResolver.php')) {
return $content;
Expand All @@ -79,20 +73,6 @@ function (string $filePath, string $prefix, string $content): string {
);
},

// unprefixed SmartFileInfo
fn (string $filePath, string $prefix, string $content): string => Strings::replace(
$content,
'#' . $prefix . '\\\\Symplify\\\\SmartFileSystem\\\\SmartFileInfo#',
'Symplify\SmartFileSystem\SmartFileInfo'
),

// unprefixed PHPUnit IsEqual
fn (string $filePath, string $prefix, string $content): string => Strings::replace(
$content,
'#' . $prefix . '\\\\PHPUnit\\\\Framework\\\\Constraint\\\\IsEqual#',
'PHPUnit\Framework\Constraint\IsEqual'
),

// fixes https://github.com/rectorphp/rector/issues/7017
function (string $filePath, string $prefix, string $content): string {
if (str_ends_with($filePath, 'vendor/symfony/string/ByteString.php')) {
Expand All @@ -106,34 +86,6 @@ function (string $filePath, string $prefix, string $content): string {
return $content;
},

// unprefixed ContainerConfigurator
function (string $filePath, string $prefix, string $content): string {
// keep vendor prefixed the prefixed file loading; not part of public API
// except @see https://github.com/symfony/symfony/commit/460b46f7302ec7319b8334a43809523363bfef39#diff-1cd56b329433fc34d950d6eeab9600752aa84a76cbe0693d3fab57fed0f547d3R110
if (str_contains($filePath, 'vendor/symfony') && ! str_ends_with(
$filePath,
'vendor/symfony/dependency-injection/Loader/PhpFileLoader.php'
)) {
return $content;
}

return Strings::replace(
$content,
'#' . $prefix . '\\\\Symfony\\\\Component\\\\DependencyInjection\\\\Loader\\\\Configurator\\\\ContainerConfigurator#',
'Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator'
);
},

// get version for prefixed version
function (string $filePath, string $prefix, string $content): string {
if (! \str_ends_with($filePath, 'src/Configuration/Configuration.php')) {
return $content;
}

// @see https://regex101.com/r/gLefQk/1
return Strings::replace($content, '#\(\'rector\/rector-src\'\)#', "('rector/rector')");
},

// un-prefix composer plugin
function (string $filePath, string $prefix, string $content): string {
if (! \str_ends_with($filePath, 'vendor/rector/extension-installer/src/Plugin.php')) {
Expand All @@ -144,27 +96,6 @@ function (string $filePath, string $prefix, string $content): string {
return Strings::replace($content, '#' . $prefix . '\\\\Composer\\\\#', 'Composer\\');
},

// fixes https://github.com/rectorphp/rector/issues/6007 - this is fixed in scoper 0.17.2+
function (string $filePath, string $prefix, string $content): string {
if (! \str_contains($filePath, 'vendor/')) {
return $content;
}

// @see https://regex101.com/r/lBV8IO/2
$fqcnReservedPattern = sprintf('#(\\\\)?%s\\\\(parent|self|static)#m', $prefix);
$matches = Strings::matchAll($content, $fqcnReservedPattern);

if ($matches === []) {
return $content;
}

foreach ($matches as $match) {
$content = str_replace($match[0], $match[2], $content);
}

return $content;
},

// 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, expect rector packages
Expand Down Expand Up @@ -194,28 +125,5 @@ function (string $filePath, string $prefix, string $content): string {

return Strings::replace($content, '#services\->load\(\'#', "services->load('" . $prefix . '\\');
},

// unprefix polyfill functions
// @see https://github.com/humbug/php-scoper/issues/440#issuecomment-795160132
function (string $filePath, string $prefix, string $content): string {
if (! Strings::match($filePath, POLYFILL_FILE_NAME_REGEX)) {
return $content;
}

return Strings::replace($content, '#namespace ' . $prefix . ';#', '');
},

// remove namespace from polyfill stubs
function (string $filePath, string $prefix, string $content): string {
if (! Strings::match($filePath, POLYFILL_STUBS_NAME_REGEX)) {
return $content;
}

// remove alias to class have origin PHP names - fix in
$content = Strings::replace($content, '#\\\\class_alias(.*?);#', '');

return Strings::replace($content, '#namespace ' . $prefix . ';#', '');
},

],
];
46 changes: 0 additions & 46 deletions utils/compiler/src/PhpScoper/StaticEasyPrefixer.php

This file was deleted.

0 comments on commit d4b7b48

Please sign in to comment.