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 64551c2..ffb22e5 100644 --- a/composer.json +++ b/composer.json @@ -1,41 +1,59 @@ { - "name": "doublesecretagency/craft-cpjs", - "description": "Add custom JavaScript to your Control Panel.", - "type": "craft-plugin", - "version": "3.0.0", - "keywords": [ - "craft", - "cms", - "craftcms", - "craft-plugin", - "cp-js", - "javascript" - ], - "support": { - "docs": "https://github.com/doublesecretagency/craft-cpjs/blob/v3/README.md", - "issues": "https://github.com/doublesecretagency/craft-cpjs/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\\cpjs\\": "src/" - } - }, - "extra": { - "name": "Control Panel JS", - "handle": "cp-js", - "schemaVersion": "2.0.0", - "changelogUrl": "https://raw.githubusercontent.com/doublesecretagency/craft-cpjs/v3/CHANGELOG.md", - "class": "doublesecretagency\\cpjs\\CpJs" + "name": "doublesecretagency/craft-cpjs", + "description": "Add custom JavaScript to your Control Panel.", + "type": "craft-plugin", + "version": "3.0.0", + "keywords": [ + "craft", + "cms", + "craftcms", + "craft-plugin", + "cp-js", + "javascript" + ], + "support": { + "docs": "https://github.com/doublesecretagency/craft-cpjs/blob/v3/README.md", + "issues": "https://github.com/doublesecretagency/craft-cpjs/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\\cpjs\\": "src/" + } + }, + "extra": { + "name": "Control Panel JS", + "handle": "cp-js", + "schemaVersion": "2.0.0", + "changelogUrl": "https://raw.githubusercontent.com/doublesecretagency/craft-cpjs/v3/CHANGELOG.md", + "class": "doublesecretagency\\cpjs\\CpJs" + }, + "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 diff --git a/src/CpJs.php b/src/CpJs.php index b146ba4..9907b63 100644 --- a/src/CpJs.php +++ b/src/CpJs.php @@ -26,7 +26,6 @@ */ class CpJs extends Plugin { - /** * @var CpJs 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 ($js) { $view->registerJs($js, View::POS_END); } - } ); } @@ -96,5 +94,4 @@ protected function settingsHtml(): ?string 'docsUrl' => $this->documentationUrl, ]); } - } diff --git a/src/models/Settings.php b/src/models/Settings.php index 4aa43ce..0d8c031 100644 --- a/src/models/Settings.php +++ b/src/models/Settings.php @@ -19,7 +19,6 @@ */ class Settings extends Model { - /** * @var string Path for the JS 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 dc3e35b..01f6949 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->js[] = $file; } - } } @@ -95,16 +93,7 @@ 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 JS, unable to hash contents of $file"); - // Return file without hash - return $file; - } - // Return file with hash return "{$file}?e={$hash}"; } - }