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
8 changes: 6 additions & 2 deletions .github/workflows/code_analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ jobs:
name: 'Tests'
run: vendor/bin/phpunit

-
name: 'Main run'
run: php bin/behastan

-
name: 'Check Active Classes'
run: vendor/bin/class-leak check bin src --ansi
run: vendor/bin/class-leak check bin src --skip-type="Entropy\Console\Contract\CommandInterface" --skip-type="Rector\Behastan\Contract\RuleInterface"

name: ${{ matrix.actions.name }}
runs-on: ubuntu-latest
Expand All @@ -39,7 +43,7 @@ jobs:
# see https://github.com/shivammathur/setup-php
- uses: shivammathur/setup-php@v2
with:
php-version: 8.2
php-version: 8.3
coverage: none

# composer install cache - https://github.com/ramsey/composer-install
Expand Down
65 changes: 65 additions & 0 deletions .github/workflows/downgraded_release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: Downgraded Release

on:
push:
tags:
# see https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#example-including-and-excluding-branches
- '*'

jobs:
downgrade_release:
runs-on: ubuntu-latest

steps:
- uses: "actions/checkout@v3"
with:
token: ${{ secrets.WORKFLOWS_TOKEN }}

-
uses: "shivammathur/setup-php@v2"
with:
php-version: 8.3
coverage: none

# invoke patches
- run: composer install --ansi

# but no dev packages
- run: composer update --no-dev --ansi

# get rector to "rector-local" directory, to avoid downgrading itself in the /vendor
- run: mkdir rector-local
- run: composer require rector/rector --working-dir rector-local --ansi

# downgrade to PHP 7.4
- run: rector-local/vendor/bin/rector process bin src vendor --config build/rector-downgrade-php.php --ansi

# clear the dev files
- run: rm -rf tests ecs.php phpstan.neon phpunit.xml .gitignore .editorconfig

# prefix and scope
- run: sh prefix-code.sh

# copy PHP 7.4 composer + workflows
- run: cp -r build/target-repository/. .

# clear the dev files
- run: rm -rf build prefix-code.sh full-tool-build.sh scoper.php rector.php php-scoper.phar rector-local

# setup git user
-
run: |
git config user.email "action@github.com"
git config user.name "GitHub Action"
# publish to the same repository with a new tag
# see https://tomasvotruba.com/blog/how-to-release-php-81-and-72-package-in-the-same-repository/
-
name: "Tag Downgraded Code"
run: |
# separate a "git add" to add untracked (new) files too
git add --all
git commit -m "release PHP 7.2 downgraded"

# force push tag, so there is only 1 version
git tag "${GITHUB_REF#refs/tags/}" --force
git push origin "${GITHUB_REF#refs/tags/}" --force
11 changes: 3 additions & 8 deletions bin/behastan.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
declare(strict_types=1);

use Rector\Behastan\DependencyInjection\ContainerFactory;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;

$possibleAutoloadPaths = [
// dependency
Expand All @@ -28,10 +25,8 @@
require_once $scoperAutoloadFilepath;
}

$containerFactory = new ContainerFactory();
$container = $containerFactory->create();
$container = ContainerFactory::create();
$consoleApplication = $container->make(\Entropy\Console\ConsoleApplication::class);

$application = $container->make(Application::class);

$exitCode = $application->run(new ArgvInput(), new ConsoleOutput());
$exitCode = $consoleApplication->run($argv);
exit($exitCode);
52 changes: 52 additions & 0 deletions build/build-scoped.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env bash

# inspired from https://github.com/rectorphp/rector/blob/main/build/build-rector-scoped.sh

# see https://stackoverflow.com/questions/66644233/how-to-propagate-colors-from-bash-script-to-github-action?noredirect=1#comment117811853_66644233
export TERM=xterm-color

# show errors
set -e

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


# functions
note()
{
MESSAGE=$1;
printf "\n";
echo "\033[0;33m[NOTE] $MESSAGE\033[0m";
}


# configure here
BUILD_DIRECTORY=$1
RESULT_DIRECTORY=$2

# ---------------------------

note "Starts"

# 2. scope it
note "Running scoper with '$RESULT_DIRECTORY' output directory"
wget https://github.com/humbug/php-scoper/releases/download/0.18.18/php-scoper.phar -N --no-verbose

# create directory
mkdir "$RESULT_DIRECTORY" -p

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

note "Show prefixed files in '$RESULT_DIRECTORY'"
ls -l $RESULT_DIRECTORY

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

# make bin/behastan runnable without "php"
chmod 777 "$RESULT_DIRECTORY/bin/behastan"
chmod 777 "$RESULT_DIRECTORY/bin/behastan.php"

note "Finished"
12 changes: 12 additions & 0 deletions build/rector-downgrade-php.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

return RectorConfig::configure()
->withDowngradeSets(php72: true)
->withSkip([
'*/Tests/*',
'*/tests/*',
]);
3 changes: 3 additions & 0 deletions build/target-repository/.github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# These are supported funding model platforms
github: tomasvotruba
custom: https://www.paypal.me/rectorphp
23 changes: 23 additions & 0 deletions build/target-repository/.github/workflows/bare_run.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Bare Run

on: [pull_request, push]

jobs:
bare_run:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
php_version: ['7.2', '7.4', '8.0', '8.2']

steps:
- uses: actions/checkout@v2

-
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php_version }}
coverage: none

- run: php bin/behastan
16 changes: 16 additions & 0 deletions build/target-repository/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "rector/behastan",
"description": "Static analysis for Behat definitions and context files",
"license": "MIT",
"require": {
"php": ">=7.2"
},
"bin": [
"bin/behastan"
],
"autoload": {
"psr-4": {
"Rector\\Behastan\\": "src"
}
}
}
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
],
"require": {
"php": "^8.2",
"illuminate/container": "^11.41|^12.37",
"nette/utils": "^4.0",
"entropy/entropy": "dev-main",
"nikic/php-parser": "^5.6",
"symfony/console": "^6.4|^7.0|^8.0",
"symfony/finder": "^6.4|^7.0|^8.0",
"symfony/finder": "^7.0",
"webmozart/assert": "^1.12"
},
"require-dev": {
Expand Down Expand Up @@ -40,7 +39,8 @@
"sort-packages": true,
"allow-plugins": {
"phpstan/extension-installer": true
}
},
"platform-check": false
},
"scripts": {
"check-cs": "vendor/bin/ecs check --ansi",
Expand Down
18 changes: 18 additions & 0 deletions full-tool-build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash

# add patches
composer install --ansi

# but skip dev dependencies
composer update --no-dev --ansi

# remove tests and useless files, to make downgraded, scoped and deployed codebase as small as possible
rm -rf tests

# downgrade with rector
mkdir rector-local
composer require rector/rector --working-dir rector-local
rector-local/vendor/bin/rector process bin src vendor --config build/rector-downgrade-php-72.php --ansi

# prefix
sh prefix-code.sh
47 changes: 47 additions & 0 deletions prefix-code.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env bash

# inspired from https://github.com/rectorphp/rector/blob/main/build/build-rector-scoped.sh

# see https://stackoverflow.com/questions/66644233/how-to-propagate-colors-from-bash-script-to-github-action?noredirect=1#comment117811853_66644233
export TERM=xterm-color

# show errors
set -e

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


# functions
note()
{
MESSAGE=$1;
printf "\n";
echo "\033[0;33m[NOTE] $MESSAGE\033[0m";
}

# ---------------------------

# 2. scope it
note "Downloading php-scoper 0.18.11"
wget https://github.com/humbug/php-scoper/releases/download/0.18.18/php-scoper.phar -N --no-verbose


note "Running php-scoper"

# Work around possible PHP memory limits
php -d memory_limit=-1 php-scoper.phar add-prefix bin src vendor composer.json --config scoper.php --force --ansi --output-dir scoped-code

# the output code is in "/scoped-code", lets move it up
# the local directories have to be empty to move easily
rm -r bin src vendor composer.json
mv scoped-code/* .

note "Dumping Composer Autoload"
composer dump-autoload --ansi --classmap-authoritative --no-dev

# make bin/ecs runnable without "php"
chmod 777 "bin/behastan"
chmod 777 "bin/behastan.php"

note "Finished"
6 changes: 6 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,11 @@
naming: true,
rectorPreset: true,
)
->withSkip([
\Rector\Php55\Rector\String_\StringClassNameToClassConstantRector::class => [
// keep string class names
__DIR__ . '/src/Resolver/ClassMethodMasksResolver.php',
],
])
->withImportNames()
->withSkip(['*/scoper.php', '*/Source/*', '*/Fixture/*']);
9 changes: 4 additions & 5 deletions src/Analyzer/UnusedDefinitionsAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\Behastan\Analyzer;

use Entropy\Console\Output\OutputPrinter;
use Nette\Utils\Strings;
use Rector\Behastan\DefinitionMasksExtractor;
use Rector\Behastan\UsedInstructionResolver;
Expand All @@ -13,7 +14,6 @@
use Rector\Behastan\ValueObject\Mask\RegexMask;
use Rector\Behastan\ValueObject\Mask\SkippedMask;
use Rector\Behastan\ValueObject\MaskCollection;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Finder\SplFileInfo;
use Webmozart\Assert\Assert;

Expand All @@ -28,7 +28,6 @@
private const MASK_VALUE_REGEX = '#(\:[\W\w]+)#';

public function __construct(
private SymfonyStyle $symfonyStyle,
private UsedInstructionResolver $usedInstructionResolver,
private DefinitionMasksExtractor $definitionMasksExtractor,
) {
Expand All @@ -55,11 +54,11 @@ public function analyse(array $contextFiles, array $featureFiles, MaskCollection
$maskCollection = $this->definitionMasksExtractor->extract($contextFiles);

$featureInstructions = $this->usedInstructionResolver->resolveInstructionsFromFeatureFiles($featureFiles);
$maskProgressBar = $this->symfonyStyle->createProgressBar($maskCollection->count());
//$maskProgressBar = $this->outputPrinter->createProgressBar($maskCollection->count());

$unusedMasks = [];
foreach ($maskCollection->all() as $mask) {
$maskProgressBar->advance();
// $maskProgressBar->advance();

if ($this->isMaskUsed($mask, $featureInstructions)) {
continue;
Expand All @@ -68,7 +67,7 @@ public function analyse(array $contextFiles, array $featureFiles, MaskCollection
$unusedMasks[] = $mask;
}

$maskProgressBar->finish();
// $maskProgressBar->finish();

return $unusedMasks;
}
Expand Down
Loading