From 32a0e20c1ccab0ed8e30741adcbb7d65d6076518 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Mon, 11 Mar 2024 22:15:50 -0400 Subject: [PATCH 1/3] feat: Add `ecs` & `phpstan` code analysis --- .github/workflows/code-analysis.yaml | 40 ++++++++++++ composer.json | 94 +++++++++++++++++----------- ecs.php | 13 ++++ phpstan.neon | 7 +++ 4 files changed, 116 insertions(+), 38 deletions(-) create mode 100644 .github/workflows/code-analysis.yaml create mode 100644 ecs.php create mode 100644 phpstan.neon diff --git a/.github/workflows/code-analysis.yaml b/.github/workflows/code-analysis.yaml new file mode 100644 index 0000000..adb2a41 --- /dev/null +++ b/.github/workflows/code-analysis.yaml @@ -0,0 +1,40 @@ +name: Code Analysis + +on: + pull_request: null + push: + branches: + - v3-dev + workflow_dispatch: +permissions: + contents: read +jobs: + code_analysis: + strategy: + fail-fast: false + matrix: + actions: + - name: 'PHPStan' + run: composer phpstan + - name: 'Coding Standards' + run: composer fix-cs + name: ${{ matrix.actions.name }} + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Cache Composer dependencies + uses: actions/cache@v4 + with: + path: /tmp/composer-cache + key: ${{ runner.os }}-${{ hashFiles('**/composer.lock') }} + - name: Setup PHP + id: setup-php + uses: shivammathur/setup-php@v2 + with: + php-version: 8.2 + extensions: 'ctype,curl,dom,iconv,imagick,intl,json,mbstring,openssl,pcre,pdo,reflection,spl,zip' + ini-values: post_max_size=256M, max_execution_time=180, memory_limit=512M + tools: composer:v2 + - name: Install Composer dependencies + run: composer install --no-interaction --no-ansi --no-progress + - run: ${{ matrix.actions.run }} diff --git a/composer.json b/composer.json index 3388e98..dc04703 100644 --- a/composer.json +++ b/composer.json @@ -1,41 +1,59 @@ { - "name": "doublesecretagency/craft-cpcss", - "description": "Add custom CSS to your Control Panel.", - "type": "craft-plugin", - "version": "3.0.0", - "keywords": [ - "craft", - "cms", - "craftcms", - "craft-plugin", - "cp-css", - "css" - ], - "support": { - "docs": "https://github.com/doublesecretagency/craft-cpcss/blob/v3/README.md", - "issues": "https://github.com/doublesecretagency/craft-cpcss/issues" - }, - "license": "MIT", - "authors": [ - { - "name": "Double Secret Agency", - "homepage": "https://www.doublesecretagency.com/plugins" - } - ], - "require": { - "craftcms/cms": "^5.0.0-beta", - "nystudio107/craft-code-editor": "^1.0.7" - }, - "autoload": { - "psr-4": { - "doublesecretagency\\cpcss\\": "src/" - } - }, - "extra": { - "name": "Control Panel CSS", - "handle": "cp-css", - "schemaVersion": "2.0.0", - "changelogUrl": "https://raw.githubusercontent.com/doublesecretagency/craft-cpcss/v3/CHANGELOG.md", - "class": "doublesecretagency\\cpcss\\CpCss" + "name": "doublesecretagency/craft-cpcss", + "description": "Add custom CSS to your Control Panel.", + "type": "craft-plugin", + "version": "3.0.0", + "keywords": [ + "craft", + "cms", + "craftcms", + "craft-plugin", + "cp-css", + "css" + ], + "support": { + "docs": "https://github.com/doublesecretagency/craft-cpcss/blob/v3/README.md", + "issues": "https://github.com/doublesecretagency/craft-cpcss/issues" + }, + "license": "MIT", + "authors": [ + { + "name": "Double Secret Agency", + "homepage": "https://www.doublesecretagency.com/plugins" } + ], + "require": { + "craftcms/cms": "^5.0.0-beta", + "nystudio107/craft-code-editor": "^1.0.7" + }, + "autoload": { + "psr-4": { + "doublesecretagency\\cpcss\\": "src/" + } + }, + "extra": { + "name": "Control Panel CSS", + "handle": "cp-css", + "schemaVersion": "2.0.0", + "changelogUrl": "https://raw.githubusercontent.com/doublesecretagency/craft-cpcss/v3/CHANGELOG.md", + "class": "doublesecretagency\\cpcss\\CpCss" + }, + "require-dev": { + "craftcms/ecs": "dev-main", + "craftcms/phpstan": "dev-main", + "craftcms/rector": "dev-main" + }, + "scripts": { + "phpstan": "phpstan --ansi --memory-limit=1G", + "check-cs": "ecs check --ansi", + "fix-cs": "ecs check --fix --ansi" + }, + "config": { + "allow-plugins": { + "craftcms/plugin-installer": true, + "yiisoft/yii2-composer": true + }, + "optimize-autoloader": true, + "sort-packages": true + } } diff --git a/ecs.php b/ecs.php new file mode 100644 index 0000000..85860c2 --- /dev/null +++ b/ecs.php @@ -0,0 +1,13 @@ +paths([ + __DIR__ . '/src', + __FILE__, + ]); + $ecsConfig->parallel(); + $ecsConfig->sets([SetList::CRAFT_CMS_4]); +}; diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..9ad1308 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,7 @@ +includes: + - %currentWorkingDirectory%/vendor/craftcms/phpstan/phpstan.neon + +parameters: + level: 5 + paths: + - src From 8c8fb3cb09d21d12bcd57a47f86034a21532801c Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Mon, 11 Mar 2024 22:17:30 -0400 Subject: [PATCH 2/3] refactor: ECS code cleanup --- src/CpCss.php | 5 +---- src/models/Settings.php | 2 -- src/web/assets/CustomAssets.php | 3 --- 3 files changed, 1 insertion(+), 9 deletions(-) diff --git a/src/CpCss.php b/src/CpCss.php index decf03d..a9813f0 100644 --- a/src/CpCss.php +++ b/src/CpCss.php @@ -26,7 +26,6 @@ */ class CpCss extends Plugin { - /** * @var CpCss Self-referential plugin property. */ @@ -54,7 +53,7 @@ public function init(): void Event::on( View::class, View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE, - function (TemplateEvent $event) { + function(TemplateEvent $event) { // Get view $view = Craft::$app->getView(); @@ -69,7 +68,6 @@ function (TemplateEvent $event) { if ($css) { $view->registerCss($css); } - } ); } @@ -96,5 +94,4 @@ protected function settingsHtml(): ?string 'docsUrl' => $this->documentationUrl, ]); } - } diff --git a/src/models/Settings.php b/src/models/Settings.php index 468359e..b2ed8dc 100644 --- a/src/models/Settings.php +++ b/src/models/Settings.php @@ -19,7 +19,6 @@ */ class Settings extends Model { - /** * @var string Path for the CSS file to load in the control panel. */ @@ -34,5 +33,4 @@ class Settings extends Model * @var bool Whether to enable the hash-based cache busting. */ public bool $cacheBusting = true; - } diff --git a/src/web/assets/CustomAssets.php b/src/web/assets/CustomAssets.php index 026c1e0..3f9fd57 100644 --- a/src/web/assets/CustomAssets.php +++ b/src/web/assets/CustomAssets.php @@ -24,7 +24,6 @@ */ class CustomAssets extends AssetBundle { - /** * @inheritdoc */ @@ -69,7 +68,6 @@ public function init(): void // Reference file without a hash $this->css[] = $file; } - } } @@ -106,5 +104,4 @@ private function _addHash(string $file): string // Return file with hash return "{$file}?e={$hash}"; } - } From aabb8a651350324ca82a2f3de16211029a74387e Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Mon, 11 Mar 2024 22:24:03 -0400 Subject: [PATCH 3/3] fix: `sha1()` always returns a string 97 Negated boolean expression is always false. --- src/web/assets/CustomAssets.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/web/assets/CustomAssets.php b/src/web/assets/CustomAssets.php index 3f9fd57..77b403a 100644 --- a/src/web/assets/CustomAssets.php +++ b/src/web/assets/CustomAssets.php @@ -93,14 +93,6 @@ private function _addHash(string $file): string // Get hash of contents $hash = @sha1($contents); - // If unable to hash file contents - if (!$hash) { - // Log warning - Craft::warning("Can't bust cache for CP CSS, unable to hash contents of $file"); - // Return file without hash - return $file; - } - // Return file with hash return "{$file}?e={$hash}"; }