Skip to content

Commit

Permalink
ci: upgrade tools
Browse files Browse the repository at this point in the history
  • Loading branch information
neilime committed Dec 20, 2023
1 parent bd3446b commit 33ab455
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 108 deletions.
16 changes: 13 additions & 3 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,23 @@ jobs:
restore-keys: ${{ runner.os }}-composer-

- name: ⚙️ Install dependencies
run: composer install --no-progress --prefer-dist --optimize-autoloader
run: |
composer install --no-progress --prefer-dist --optimize-autoloader
composer --working-dir=tools install --no-progress --prefer-dist --optimize-autoloader
- name: ♻️ Tools cache
uses: actions/cache@v3
with:
path: tools/cache
key: ${{ runner.os }}-test-${{ github.sha }}
restore-keys: |
${{ runner.os }}-test-
- name: 👕 Lint
run: composer cs
run: composer php-cs-fixer -- --format=checkstyle | tools/vendor/bin/cs2pr

- name: 🔬 Static analysis
run: composer stan
run: composer stan -- --error-format=checkstyle | tools/vendor/bin/cs2pr

- name: ♻️ Test cache
uses: actions/cache@v3
Expand Down
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
/.buildpath
/.project
/vendor
/tools/vendor
/tools/cache
/nbproject/
nbactions.xml
nb-configuration.xml
/build/
.idea/
composer.lock
*.swp
/website/node_modules
tests/.phpunit.result.cache
/website/node_modules
18 changes: 18 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use PhpCsFixer\Finder;
use PhpCsFixer\Config;

$finder = Finder::create()
->in(__DIR__ . '/src')
->in(__DIR__ . '/tests')
->ignoreVCSIgnored(true);

return (new Config())
->setRules([
'@PSR12' => true,
'array_indentation' => true,
])
->setFinder($finder)
->setUsingCache(true)
->setCacheFile(__DIR__ . '/tools/cache/.php-cs-fixer.cache');
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ install: ## Install PHP dependencies for given PHP version
rm -f composer.lock
@$(call run-php,$(filter-out $@,$(MAKECMDGOALS)) composer install --no-progress --prefer-dist --optimize-autoloader)
rm -f composer.lock
@$(call run-php,$(filter-out $@,$(MAKECMDGOALS)) composer --working-dir=tools install --no-progress --prefer-dist --optimize-autoloader)

shell: ## Execute shell in given PHP version container
@$(call run-php,$(filter-out $@,$(MAKECMDGOALS)) bash)
Expand All @@ -22,10 +23,10 @@ test-update: ## Execute tests and update snapshots for given PHP version
@$(call run-php,$(filter-out $@,$(MAKECMDGOALS)) composer test:update-snapshot)

lint: ## Execute lint for given PHP version
@$(call run-php,$(filter-out $@,$(MAKECMDGOALS)) composer cs)
@$(call run-php,$(filter-out $@,$(MAKECMDGOALS)) composer php-cs-fixer)

lint-fix: ## Execute lint fixing for given PHP version
@$(call run-php,$(filter-out $@,$(MAKECMDGOALS)) composer cbf)
@$(call run-php,$(filter-out $@,$(MAKECMDGOALS)) composer php-cs-fixer:fix)

stan: ## Execute PHPStan for given PHP version
@$(call run-php,$(filter-out $@,$(MAKECMDGOALS)) composer stan)
Expand Down
13 changes: 4 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,8 @@
"laminas/laminas-mvc": "^3.3",
"mikey179/vfsstream": "^1.6",
"pcov/clobber": "^2.0",
"phpstan/extension-installer": "^1.2",
"phpstan/phpstan": "^1.9",
"phpstan/phpstan-phpunit": "^1.2",
"phpunit/phpunit": "^9.5.26",
"slam/phpstan-laminas-framework": "^1.3",
"spatie/phpunit-snapshot-assertions": "^4.2",
"squizlabs/php_codesniffer": "^3.7",
"twbs/bootstrap": "^5.1"
},
"autoload": {
Expand All @@ -69,11 +64,11 @@
"test:ci": "@test -d pcov.enabled=1 -d max_execution_time=0 --coverage-text --coverage-clover ./build/logs/clover.xml --coverage-html ./build/coverage/",
"generate-docs": "php scripts/generateDocumentation.php",
"generate-snapshot": "php scripts/generateSnapshot.php",
"cs": "phpcs",
"cbf": "phpcbf",
"stan": "phpstan analyse --level 5 src",
"php-cs-fixer": "@php-cs-fixer:fix --dry-run",
"php-cs-fixer:fix": "tools/vendor/bin/php-cs-fixer fix --show-progress=dots --diff --config=.php-cs-fixer.dist.php",
"stan": "tools/vendor/bin/phpstan analyse --level 5 src",
"ci": [
"@cs",
"@php-cs-fixer",
"@stan",
"@test:ci"
]
Expand Down
19 changes: 0 additions & 19 deletions phpcs.xml

This file was deleted.

1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
parameters:
tmpDir: tools/cache/phpstan
laminasframework:
serviceManagerLoader: tests/phpstan.php
excludePaths:
Expand Down
67 changes: 36 additions & 31 deletions src/Documentation/Generator/UsagePage/Prettifier/PhpPrettifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,22 @@

namespace Documentation\Generator\UsagePage\Prettifier;

use PhpCsFixer\Console\Application;

class PhpPrettifier
{
private static $CONFIGURATION_FILE = '.php-cs-fixer.dist.php';
private static $instance = null;

private static $CONFIGURATION_PATH = 'phpcs.xml';

private $config;
private $ruleset;
private $configurationPath = '.php-cs-fixer.dist.php';
private $application;

private function __construct(\Documentation\Generator\Configuration $configuration)
{
if (defined('PHP_CODESNIFFER_VERBOSITY') === false) {
define('PHP_CODESNIFFER_VERBOSITY', false);
}
if (defined('PHP_CODESNIFFER_CBF') === false) {
define('PHP_CODESNIFFER_CBF', true);
}

$config = new \PHP_CodeSniffer\Config([
$configuration->getRootDirPath() . DIRECTORY_SEPARATOR . self::$CONFIGURATION_PATH
]);
$config->stdin = true;
$config->standards = ['Squiz'];
$this->configurationPath = $configuration->getRootDirPath() . DIRECTORY_SEPARATOR . self::$CONFIGURATION_FILE;

$runner = new \PHP_CodeSniffer\Runner();
$runner->config = $config;
$runner->init();

$this->config = $config;
$this->ruleset = $runner->ruleset;
$this->application = new Application();
$this->application->setAutoExit(false);
}

/**
Expand All @@ -50,17 +36,36 @@ public static function getInstance(

public function prettify($source)
{
// Write source to temporary file
$tmpFile = tempnam(sys_get_temp_dir(), 'phpcsfixer');
file_put_contents($tmpFile, $source);

try {
$input = new \Symfony\Component\Console\Input\ArrayInput(
[
'command' => 'fix',
'path' => [$tmpFile],
'--dry-run' => false,
'--config' => $this->configurationPath,
]
);

$output = new \Symfony\Component\Console\Output\BufferedOutput();

$this->config->stdinContent = $source;
$file = new \Documentation\Generator\UsagePage\Prettifier\SourceFile(
$source,
$this->ruleset,
$this->config
);
$result = $this->application->run(
$input,
$output
);

$file->process();
$file->fixer->fixFile();
if ($result !== 0) {
throw new \RuntimeException('PhpCsFixer failed.' . $output->fetch());
}

return $file->getContent();
$prettyfiedSource = trim(file_get_contents($tmpFile));

return $prettyfiedSource;
} finally {
unlink($tmpFile);
}
}
}
15 changes: 6 additions & 9 deletions tests/Bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
<?php

// phpcs:disable
namespace TestSuite;


class Bootstrap
{

/**
* @var \Laminas\ServiceManager\ServiceManager
*/
Expand Down Expand Up @@ -98,15 +95,15 @@ protected static function findParentPath($path)
));
}

// PHP Code Sniffer autoloading
if (!file_exists($phpCodeSnifferAutoloadPath = __DIR__ . '/../vendor/squizlabs/php_codesniffer/autoload.php')) {
throw new \LogicException('PHP Code Sniffer autoload file "' . $phpCodeSnifferAutoloadPath . '" does not exist');
// Tools autoloading
if (!file_exists($toolsAutoloadPath = __DIR__ . '/../tools/vendor/autoload.php')) {
throw new \LogicException('Tools autoload file "' . $toolsAutoloadPath . '" does not exist');
}

if (false === (include $phpCodeSnifferAutoloadPath)) {
if (false === (include $toolsAutoloadPath)) {
throw new \LogicException(sprintf(
'An error occured while including PHP Code Sniffer autoload file "%s"',
$phpCodeSnifferAutoloadPath
'An error occured while including tools autoload file "%s"',
$toolsAutoloadPath
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,24 @@ protected function setUp(): void
public function testPrettifyShouldSetProperIndentation()
{
$source = <<<'SOURCE'
<?php
$factory = new \Laminas\Form\Factory();
// Render Default checkbox
echo $this->formRow($factory->create([
'name' => 'default-checkbox',
'type' => 'checkbox',
'options' => [
'label' => 'Default checkbox',
'use_hidden_element' => false,
'form_group' => false,
],
'attributes' => [
'id' => 'defaultCheck1',
],
])) . PHP_EOL;
SOURCE;
<?php
$factory = new \Laminas\Form\Factory();
// Render Default checkbox
echo $this->formRow($factory->create([
'name' => 'default-checkbox',
'type' => 'checkbox',
'options' => [
'label' => 'Default checkbox',
'use_hidden_element' => false,
'form_group' => false,
],
'attributes' => [
'id' => 'defaultCheck1',
],
])) . PHP_EOL;
SOURCE;

$result = $this->phpPrettifier->prettify($source);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
$factory = new \Laminas\Form\Factory();

// Render Default checkbox
echo $this->formRow(
$factory->create(
[
'name' => 'default-checkbox',
'type' => 'checkbox',
'options' => [
'label' => 'Default checkbox',
'use_hidden_element' => false,
'form_group' => false,
],
'attributes' => [ 'id' => 'defaultCheck1' ],
]
)
).PHP_EOL;
echo $this->formRow($factory->create([
'name' => 'default-checkbox',
'type' => 'checkbox',
'options' => [
'label' => 'Default checkbox',
'use_hidden_element' => false,
'form_group' => false,
],
'attributes' => [
'id' => 'defaultCheck1',
],
])) . PHP_EOL;
2 changes: 1 addition & 1 deletion tests/phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="Bootstrap.php" colors="true" verbose="true" backupGlobals="false" backupStaticAttributes="false" stopOnFailure="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="Bootstrap.php" colors="true" verbose="true" backupGlobals="false" backupStaticAttributes="false" stopOnFailure="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" cacheResultFile="../tools/cache/.phpunit.result.cache">
<coverage>
<include>
<directory suffix=".php">../src/</directory>
Expand Down
2 changes: 2 additions & 0 deletions tools/cache/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
16 changes: 16 additions & 0 deletions tools/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"require": {
"friendsofphp/php-cs-fixer": "^3.41",
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-phpunit": "^1.3",
"slam/phpstan-laminas-framework": "^1.5",
"staabm/annotate-pull-request-from-checkstyle": "^1.8",
"slam/php-cs-fixer-extensions": "^3.10"
},
"config": {
"allow-plugins": {
"phpstan/extension-installer": true
}
}
}

0 comments on commit 33ab455

Please sign in to comment.