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
37 changes: 37 additions & 0 deletions .github/workflows/code-quality.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Code Quality

on:
pull_request:
push:
branches:
- main

jobs:
codeQuality:
runs-on: ubuntu-latest
name: PHP
steps:
- name: Cancel previous incomplete runs
uses: styfle/cancel-workflow-action@0.12.1
with:
access_token: ${{ github.token }}

- name: Checkout changes
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Install PHP and PHP Code Sniffer
uses: shivammathur/setup-php@v2
with:
php-version: 8.2
extensions: curl, fileinfo, gd, mbstring, openssl, pdo, pdo_sqlite, sqlite3, xml, zip
tools: phpcs

- name: Run code quality checks (on push)
if: github.event_name == 'push'
run: ./.github/workflows/utilities/phpcs-push ${{ github.sha }}

- name: Run code quality checks (on pull request)
if: github.event_name == 'pull_request'
run: ./.github/workflows/utilities/phpcs-pr ${{ github.base_ref }}
83 changes: 83 additions & 0 deletions .github/workflows/utilities/phpcs-pr
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env php
<?php
/**
* Run PHPCS tests against a PR.
*
* The only argument is for the PR's base branch, which is then compared to the HEAD of the PR to retrieve the list
* of changed files. The PHPCS tests are only run against these changed files, to speed up the tests.
*/
if (empty($argv[1])) {
echo 'You must provide a base branch to check this PR against.';
echo "\n";
exit(1);
}

// Get a changelist of files from Git for this PR.
$fileList = shell_exec('git diff --name-only --diff-filter=ACMR origin/' . $argv[1] . ' HEAD');
$files = array_filter(explode("\n", $fileList));

foreach ($files as &$file) {
if (strpos($file, ' ') !== false) {
$file = str_replace(' ', '\\ ', $file);
}
}

// Run all changed files through the PHPCS code sniffer and generate a CSV report
$csv = shell_exec('phpcs --colors -nq --report="csv" --extensions="php" ' . implode(' ', $files));
$lines = array_map(function ($row) {
return array_map(function ($column) {
return trim($column, '"');
}, explode(',', $row));
}, array_filter(explode("\n", $csv)));

// Remove header row
array_shift($lines);

if (!count($lines)) {
echo "\e[0;32mFound no issues with code quality.\e[0m";
echo "\n";
exit(0);
} else {
// Group errors by file
$files = [];

foreach ($lines as $line) {
$filename = str_replace(dirname(dirname(dirname(__DIR__))), '', $line[0]);

if (empty($files[$filename])) {
$files[$filename] = [];
}

$files[$filename][] = [
'warning' => ($line[3] === 'warning'),
'message' => $line[4],
'line' => $line[1],
];
}

// Render report
echo "\e[0;31mFound "
. ((count($lines) === 1)
? '1 issue'
: count($lines) . ' issues')
. " with code quality.\e[0m";
echo "\n";

foreach ($files as $file => $errors) {
echo "\n";
echo "\e[1;37m" . str_replace('"', '', $file) . "\e[0m";
echo "\n\n";

foreach ($errors as $error) {
echo "\e[2m" . str_pad(' L' . $error['line'], 7) . " | \e[0m";
if ($error['warning'] === false) {
echo "\e[0;31mERR:\e[0m ";
} else {
echo "\e[1;33mWARN:\e[0m ";
}
echo $error['message'];
echo "\n";
}
}
exit(1);
}
83 changes: 83 additions & 0 deletions .github/workflows/utilities/phpcs-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env php
<?php
/**
* Run PHPCS tests against a push.
*
* The only argument is for the commit, which a list of changed files is retrieved from. The PHPCS tests are only run
* against these changed files, to speed up the tests.
*/
if (empty($argv[1])) {
echo 'You must provide a commit SHA to check.';
echo "\n";
exit(1);
}

// Get a changelist of files from Git for this push.
$fileList = shell_exec('git show --name-only --pretty="" --diff-filter=ACMR ' . $argv[1]);
$files = array_filter(explode("\n", $fileList));

foreach ($files as &$file) {
if (strpos($file, ' ') !== false) {
$file = str_replace(' ', '\\ ', $file);
}
}

// Run all changed files through the PHPCS code sniffer and generate a CSV report
$csv = shell_exec('phpcs --colors -nq --report="csv" --extensions="php" ' . implode(' ', $files));
$lines = array_map(function ($row) {
return array_map(function ($column) {
return trim($column, '"');
}, explode(',', $row));
}, array_filter(explode("\n", $csv)));

// Remove header row
array_shift($lines);

if (!count($lines)) {
echo "\e[0;32mFound no issues with code quality.\e[0m";
echo "\n";
exit(0);
} else {
// Group errors by file
$files = [];

foreach ($lines as $line) {
$filename = str_replace(dirname(dirname(dirname(__DIR__))), '', $line[0]);

if (empty($files[$filename])) {
$files[$filename] = [];
}

$files[$filename][] = [
'warning' => ($line[3] === 'warning'),
'message' => $line[4],
'line' => $line[1],
];
}

// Render report
echo "\e[0;31mFound "
. ((count($lines) === 1)
? '1 issue'
: count($lines) . ' issues')
. " with code quality.\e[0m";
echo "\n";

foreach ($files as $file => $errors) {
echo "\n";
echo "\e[1;37m" . str_replace('"', '', $file) . "\e[0m";
echo "\n\n";

foreach ($errors as $error) {
echo "\e[2m" . str_pad(' L' . $error['line'], 7) . " | \e[0m";
if ($error['warning'] === false) {
echo "\e[0;31mERR:\e[0m ";
} else {
echo "\e[1;33mWARN:\e[0m ";
}
echo $error['message'];
echo "\n";
}
}
exit(1);
}
3 changes: 3 additions & 0 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Winter\Blocks\Classes\BlockManager;
use Winter\Blocks\Classes\BlocksDatasource;
use Winter\Blocks\Classes\Block as BlockModel;
use Winter\Blocks\Console\ScaffoldCommand;
use Winter\Blocks\FormWidgets\Block;

/**
Expand Down Expand Up @@ -110,6 +111,8 @@ public function boot(): void
*/
public function register(): void
{
$this->registerConsoleCommand('winter.blocks.scaffold', ScaffoldCommand::class);

Event::listen('cms.theme.registerHalcyonDatasource', function (Theme $theme, $resolver) {
BlockManager::instance(); // moved here

Expand Down
Loading
Loading