From 2c2d85df085abb3f182457303e7d8f01142b2bc5 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Thu, 25 Jan 2024 15:45:04 -0500 Subject: [PATCH 01/10] docs: Use links to the production branch --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8292848..292d8b5 100755 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/badges/quality-score.png?b=develop)](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/?branch=develop) [![Code Coverage](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/badges/coverage.png?b=develop)](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/?branch=develop) [![Build Status](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/badges/build.png?b=develop)](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/build-status/develop) [![Code Intelligence Status](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/badges/code-intelligence.svg?b=develop)](https://scrutinizer-ci.com/code-intelligence) +[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/badges/quality-score.png?b=v5)](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/?branch=v5) [![Code Coverage](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/badges/coverage.png?b=v5)](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/?branch=v5) [![Build Status](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/badges/build.png?b=v5)](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/build-status/v5) [![Code Intelligence Status](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/badges/code-intelligence.svg?b=v5)](https://scrutinizer-ci.com/code-intelligence) # Code Editor for Craft CMS 3.x & 4.x @@ -445,7 +445,7 @@ The Monaco editor that the Code Editor is based on supports [JSON Schema](https: You can play with an example of this in the [Monaco playground](https://microsoft.github.io/monaco-editor/playground.html?source=v0.37.1#example-extending-language-services-configure-json-defaults) -Code Editor adds some support to make it a bit easier to do, here's an example from the Craft [Code Field plugin](https://github.com/nystudio107/craft-code-field) on one way to do it via a [Twig template](https://github.com/nystudio107/craft-code-field/blob/develop-v3/src/templates/_components/fields/Code_settings.twig#L162): +Code Editor adds some support to make it a bit easier to do, here's an example from the Craft [Code Field plugin](https://github.com/nystudio107/craft-code-field) on one way to do it via a [Twig template](https://github.com/nystudio107/craft-code-field/blob/v5/src/templates/_components/fields/Code_settings.twig#L161): ```js {% js %} @@ -464,7 +464,7 @@ monaco.languages.json.jsonDefaults.setDiagnosticsOptions({ {% endjs %} ``` -...where the `optionsSchema` variable is [injected into the template](https://github.com/nystudio107/craft-code-field/blob/develop-v3/src/fields/Code.php#L161), and contains the contents of the [`IEditorOptionsSchema.json`](https://github.com/nystudio107/craft-code-field/blob/develop-v3/src/resources/IEditorOptionsSchema.json) file. +...where the `optionsSchema` variable is [injected into the template](https://github.com/nystudio107/craft-code-field/blob/v5/src/fields/Code.php#L161), and contains the contents of the [`IEditorOptionsSchema.json`](https://github.com/nystudio107/craft-code-field/blob/v5/src/resources/IEditorOptionsSchema.json) file. You could alternatively grab this file via an XHR from an Asset Bundle, or just inline the schema definition directly. From edd8df29b5cdf852815f62aa1893442eb3a05529 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Thu, 25 Jan 2024 15:45:11 -0500 Subject: [PATCH 02/10] docs: Use links to the production branch --- README.md | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 8292848..f231810 100755 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/badges/quality-score.png?b=develop)](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/?branch=develop) [![Code Coverage](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/badges/coverage.png?b=develop)](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/?branch=develop) [![Build Status](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/badges/build.png?b=develop)](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/build-status/develop) [![Code Intelligence Status](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/badges/code-intelligence.svg?b=develop)](https://scrutinizer-ci.com/code-intelligence) +[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/badges/quality-score.png?b=v4)](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/?branch=v4) [![Code Coverage](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/badges/coverage.png?b=v4)](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/?branch=v4) [![Build Status](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/badges/build.png?b=v4)](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/build-status/v4) [![Code Intelligence Status](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/badges/code-intelligence.svg?b=v4)](https://scrutinizer-ci.com/code-intelligence) # Code Editor for Craft CMS 3.x & 4.x @@ -52,7 +52,7 @@ It can be very easy to add to an existing project, as you can see from the [Prep ### In the Craft CP -Code Editor works just like the Craft CMS `forms` macros that should be familiar to plugin and module developers. +Code Editor works just like the Craft CMS `forms` macros that should be familiar to plugin and module v4ers. #### Import Macros @@ -330,7 +330,7 @@ Note that all of the above examples _add_ Autocompletes to the Autocompletes tha ## Writing a Custom Autocomplete -Autocompletes extend from the base [Autocomplete](https://github.com/nystudio107/craft-code-editor/blob/develop/src/base/Autocomplete.php) class, and implement the [AutocompleteInterface](https://github.com/nystudio107/craft-code-editor/blob/develop/src/base/AutocompleteInterface.php) +Autocompletes extend from the base [Autocomplete](https://github.com/nystudio107/craft-code-editor/blob/v4/src/base/Autocomplete.php) class, and implement the [AutocompleteInterface](https://github.com/nystudio107/craft-code-editor/blob/v4/src/base/AutocompleteInterface.php) A simple Autocomplete would look like this: @@ -376,18 +376,18 @@ Your Autocomplete also has a `$codeEditorOptions` property which will contain an See the following examples for custom Autocompletes that you can use as a guide when creating your own: -* [TrackingVarsAutocomplete](https://github.com/nystudio107/craft-seomatic/blob/develop/src/autocompletes/TrackingVarsAutocomplete.php) -* [SprigApiAutocomplete](https://github.com/putyourlightson/craft-sprig/blob/develop/src/autocompletes/SprigApiAutocomplete.php) -* [CraftApiAutocomplete](https://github.com/nystudio107/craft-code-editor/blob/develop/src/autocompletes/CraftApiAutocomplete.php) -* [EnvironmentVariableAutocomplete](https://github.com/nystudio107/craft-code-editor/blob/develop/src/autocompletes/EnvironmentVariableAutocomplete.php) -* [TwigLanguageAutocomplete](https://github.com/nystudio107/craft-code-editor/blob/develop/src/autocompletes/TwigLanguageAutocomplete.php) +* [TrackingVarsAutocomplete](https://github.com/nystudio107/craft-seomatic/blob/v4/src/autocompletes/TrackingVarsAutocomplete.php) +* [SprigApiAutocomplete](https://github.com/putyourlightson/craft-sprig/blob/v4/src/autocompletes/SprigApiAutocomplete.php) +* [CraftApiAutocomplete](https://github.com/nystudio107/craft-code-editor/blob/v4/src/autocompletes/CraftApiAutocomplete.php) +* [EnvironmentVariableAutocomplete](https://github.com/nystudio107/craft-code-editor/blob/v4/src/autocompletes/EnvironmentVariableAutocomplete.php) +* [TwigLanguageAutocomplete](https://github.com/nystudio107/craft-code-editor/blob/v4/src/autocompletes/TwigLanguageAutocomplete.php) ## Twig Template Validators Code Editor also includes two Twig template [Validators](https://www.yiiframework.com/doc/guide/2.0/en/tutorial-core-validators) that you can use to validate Twig templates that are saved as part of a model: -* [TwigTemplateValidator](https://github.com/nystudio107/craft-code-editor/blob/develop/src/validators/TwigTemplateValidator.php) - validates the template via `renderString()` -* [TwigObjectTemplateValidator](https://github.com/nystudio107/craft-code-editor/blob/develop/src/validators/TwigObjectTemplateValidator.php) - validates the template via `renderObjectTemplate()` +* [TwigTemplateValidator](https://github.com/nystudio107/craft-code-editor/blob/v4/src/validators/TwigTemplateValidator.php) - validates the template via `renderString()` +* [TwigObjectTemplateValidator](https://github.com/nystudio107/craft-code-editor/blob/v4/src/validators/TwigObjectTemplateValidator.php) - validates the template via `renderObjectTemplate()` You just add them as a rule on your model, and it will propagate the model with any errors that were encountered when rendering the template: @@ -445,7 +445,7 @@ The Monaco editor that the Code Editor is based on supports [JSON Schema](https: You can play with an example of this in the [Monaco playground](https://microsoft.github.io/monaco-editor/playground.html?source=v0.37.1#example-extending-language-services-configure-json-defaults) -Code Editor adds some support to make it a bit easier to do, here's an example from the Craft [Code Field plugin](https://github.com/nystudio107/craft-code-field) on one way to do it via a [Twig template](https://github.com/nystudio107/craft-code-field/blob/develop-v3/src/templates/_components/fields/Code_settings.twig#L162): +Code Editor adds some support to make it a bit easier to do, here's an example from the Craft [Code Field plugin](https://github.com/nystudio107/craft-code-field) on one way to do it via a [Twig template](https://github.com/nystudio107/craft-code-field/blob/v4/src/templates/_components/fields/Code_settings.twig#L161): ```js {% js %} @@ -464,7 +464,7 @@ monaco.languages.json.jsonDefaults.setDiagnosticsOptions({ {% endjs %} ``` -...where the `optionsSchema` variable is [injected into the template](https://github.com/nystudio107/craft-code-field/blob/develop-v3/src/fields/Code.php#L161), and contains the contents of the [`IEditorOptionsSchema.json`](https://github.com/nystudio107/craft-code-field/blob/develop-v3/src/resources/IEditorOptionsSchema.json) file. +...where the `optionsSchema` variable is [injected into the template](https://github.com/nystudio107/craft-code-field/blob/v4/src/fields/Code.php#L163), and contains the contents of the [`IEditorOptionsSchema.json`](https://github.com/nystudio107/craft-code-field/blob/v4/src/resources/IEditorOptionsSchema.json) file. You could alternatively grab this file via an XHR from an Asset Bundle, or just inline the schema definition directly. From 7f142630ea1b52a6e253599220d888a5671ae884 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Thu, 25 Jan 2024 15:55:16 -0500 Subject: [PATCH 03/10] chore: Update versions to add 5.x --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 292d8b5..4d660d6 100755 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/badges/quality-score.png?b=v5)](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/?branch=v5) [![Code Coverage](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/badges/coverage.png?b=v5)](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/?branch=v5) [![Build Status](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/badges/build.png?b=v5)](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/build-status/v5) [![Code Intelligence Status](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/badges/code-intelligence.svg?b=v5)](https://scrutinizer-ci.com/code-intelligence) -# Code Editor for Craft CMS 3.x & 4.x +# Code Editor for Craft CMS 3.x, 4.x & 5.0 Provides a code editor field with Twig & Craft API autocomplete @@ -8,7 +8,7 @@ Provides a code editor field with Twig & Craft API autocomplete ## Requirements -Code Editor requires Craft CMS 3.0 or 4.0. +Code Editor requires Craft CMS 3.x, 4.x or 5.x. ## Installation From e3b7fe7c4cd5bc1b2df17792abbd57f3512082d7 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Mon, 29 Jan 2024 20:39:40 -0500 Subject: [PATCH 04/10] feat: Add `phpstan` and `ecs` code linting --- composer.json | 12 ++++++++++-- ecs.php | 13 +++++++++++++ phpstan.neon | 7 +++++++ 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 ecs.php create mode 100644 phpstan.neon diff --git a/composer.json b/composer.json index 1782d83..e45b841 100755 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "nystudio107/craft-code-editor", "description": "Provides a code editor field with Twig & Craft API autocomplete", "type": "yii2-extension", - "version": "1.0.16", + "version": "1.0.17", "keywords": [ "code", "editor", @@ -28,10 +28,18 @@ } ], "require": { + "craftcms/cms": "^3.0.0 || ^4.0.0 || ^5.0.0-alpha.1", "phpdocumentor/reflection-docblock": "^5.0.0" }, "require-dev": { - "craftcms/cms": "^3.0.0 || ^4.0.0 || ^5.0.0-alpha.1" + "craftcms/ecs": "dev-main", + "craftcms/phpstan": "dev-main", + "craftcms/rector": "dev-main" + }, + "scripts": { + "phpstan": "vendor/bin/phpstan --ansi --memory-limit=1G", + "check-cs": "ecs check --ansi", + "fix-cs": "ecs check --fix --ansi" }, "config": { "allow-plugins": { 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..44554e4 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,7 @@ +includes: + - vendor/craftcms/phpstan/phpstan.neon + +parameters: + level: 5 + paths: + - src From e8ef87615e4d8c5e3661fa22a984201fe3a7f97d Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Mon, 29 Jan 2024 20:40:05 -0500 Subject: [PATCH 05/10] feat: Add `code-analysis.yaml` GitHub action --- .github/workflows/code-analysis.yaml | 34 ++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 .github/workflows/code-analysis.yaml diff --git a/.github/workflows/code-analysis.yaml b/.github/workflows/code-analysis.yaml new file mode 100644 index 0000000..ce51408 --- /dev/null +++ b/.github/workflows/code-analysis.yaml @@ -0,0 +1,34 @@ +name: Code Analysis + +on: + pull_request: null + push: + branches: + - develop + 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 + # see https://github.com/shivammathur/setup-php + - uses: shivammathur/setup-php@v2 + with: + php-version: 8.2 + coverage: none + # composer install cache - https://github.com/ramsey/composer-install + - uses: "ramsey/composer-install@v2" + - run: ${{ matrix.actions.run }} From 74ac1734d0ac0b094ff144103b9e9b6feac87ca7 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Mon, 29 Jan 2024 20:40:14 -0500 Subject: [PATCH 06/10] refactor: Remove year --- LICENSE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE.md b/LICENSE.md index 51d2615..16af3d5 100755 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2022 nystudio107 +Copyright (c) nystudio107 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: From 501bcf7ae5b1276b73b9f5478b54cdb43d77f6b8 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Mon, 29 Jan 2024 20:40:37 -0500 Subject: [PATCH 07/10] refactor: PHPstan code cleanup --- src/autocompletes/CraftApiAutocomplete.php | 2 +- src/autocompletes/EnvironmentVariableAutocomplete.php | 2 +- src/autocompletes/ObjectAutocomplete.php | 2 +- src/autocompletes/SectionShorthandFieldsAutocomplete.php | 2 +- src/autocompletes/TwigLanguageAutocomplete.php | 2 +- src/base/ObjectParserAutocomplete.php | 9 ++++++--- src/services/AutocompleteService.php | 8 ++++---- 7 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/autocompletes/CraftApiAutocomplete.php b/src/autocompletes/CraftApiAutocomplete.php index 07442a2..651933c 100644 --- a/src/autocompletes/CraftApiAutocomplete.php +++ b/src/autocompletes/CraftApiAutocomplete.php @@ -46,7 +46,7 @@ class CraftApiAutocomplete extends ObjectParserAutocomplete public $type = AutocompleteTypes::TwigExpressionAutocomplete; /** - * @var string Whether the autocomplete should be parsed with . -delimited nested sub-properties + * @var bool Whether the autocomplete should be parsed with . -delimited nested sub-properties */ public $hasSubProperties = true; diff --git a/src/autocompletes/EnvironmentVariableAutocomplete.php b/src/autocompletes/EnvironmentVariableAutocomplete.php index c08da85..7ee32f6 100644 --- a/src/autocompletes/EnvironmentVariableAutocomplete.php +++ b/src/autocompletes/EnvironmentVariableAutocomplete.php @@ -37,7 +37,7 @@ class EnvironmentVariableAutocomplete extends Autocomplete public $type = AutocompleteTypes::GeneralAutocomplete; /** - * @var string Whether the autocomplete should be parsed with . -delimited nested sub-properties + * @var bool Whether the autocomplete should be parsed with . -delimited nested sub-properties */ public $hasSubProperties = false; diff --git a/src/autocompletes/ObjectAutocomplete.php b/src/autocompletes/ObjectAutocomplete.php index e5782c5..4e958d1 100644 --- a/src/autocompletes/ObjectAutocomplete.php +++ b/src/autocompletes/ObjectAutocomplete.php @@ -35,7 +35,7 @@ class ObjectAutocomplete extends ObjectParserAutocomplete public $type = AutocompleteTypes::TwigExpressionAutocomplete; /** - * @var string Whether the autocomplete should be parsed with . -delimited nested sub-properties + * @var bool Whether the autocomplete should be parsed with . -delimited nested sub-properties */ public $hasSubProperties = true; diff --git a/src/autocompletes/SectionShorthandFieldsAutocomplete.php b/src/autocompletes/SectionShorthandFieldsAutocomplete.php index d742a32..9156f10 100644 --- a/src/autocompletes/SectionShorthandFieldsAutocomplete.php +++ b/src/autocompletes/SectionShorthandFieldsAutocomplete.php @@ -57,7 +57,7 @@ class SectionShorthandFieldsAutocomplete extends ObjectParserAutocomplete public $type = AutocompleteTypes::TwigExpressionAutocomplete; /** - * @var string Whether the autocomplete should be parsed with . -delimited nested sub-properties + * @var bool Whether the autocomplete should be parsed with . -delimited nested sub-properties */ public $hasSubProperties = true; diff --git a/src/autocompletes/TwigLanguageAutocomplete.php b/src/autocompletes/TwigLanguageAutocomplete.php index 7b4ce89..0a06bfb 100644 --- a/src/autocompletes/TwigLanguageAutocomplete.php +++ b/src/autocompletes/TwigLanguageAutocomplete.php @@ -248,7 +248,7 @@ class TwigLanguageAutocomplete extends Autocomplete public $type = AutocompleteTypes::TwigExpressionAutocomplete; /** - * @var string Whether the autocomplete should be parsed with . -delimited nested sub-properties + * @var bool Whether the autocomplete should be parsed with . -delimited nested sub-properties */ public $hasSubProperties = false; diff --git a/src/base/ObjectParserAutocomplete.php b/src/base/ObjectParserAutocomplete.php index 7df8660..5457deb 100644 --- a/src/base/ObjectParserAutocomplete.php +++ b/src/base/ObjectParserAutocomplete.php @@ -251,7 +251,7 @@ protected function getPropertyCompletion($object, DocBlockFactory $factory, $rec if ((PHP_MAJOR_VERSION >= 7 && PHP_MINOR_VERSION >= 4) || (PHP_MAJOR_VERSION >= 8)) { if ($reflectionProperty->hasType()) { $reflectionType = $reflectionProperty->getType(); - if ($reflectionType instanceof ReflectionNamedType) { + if ($reflectionType instanceof \ReflectionNamedType) { $type = $reflectionType->getName(); $detail = $type; } @@ -314,13 +314,16 @@ protected function getMethodCompletion($object, DocBlockFactory $factory, string if ($methodAllowed && $reflectionMethod->isPublic()) { $docblock = null; $docs = $this->getDocs($reflectionMethod, $factory); + if (!empty($docs)) { + $docblock = $factory->create($docs); + } $detail = $methodName . '('; $params = $reflectionMethod->getParameters(); $paramList = []; foreach ($params as $param) { if ($param->hasType()) { $reflectionType = $param->getType(); - if ($reflectionType instanceof ReflectionUnionType) { + if ($reflectionType instanceof \ReflectionUnionType) { $unionTypes = $reflectionType->getTypes(); $typeName = ''; foreach ($unionTypes as $unionType) { @@ -328,7 +331,7 @@ protected function getMethodCompletion($object, DocBlockFactory $factory, string } $typeName = trim($typeName, '|'); $paramList[] = $typeName . ': ' . '$' . $param->getName(); - } else { + } else if ($param->getType() instanceof \ReflectionNamedType) { $paramList[] = $param->getType()->getName() . ': ' . '$' . $param->getName(); } } else { diff --git a/src/services/AutocompleteService.php b/src/services/AutocompleteService.php index 4a29537..f32c674 100755 --- a/src/services/AutocompleteService.php +++ b/src/services/AutocompleteService.php @@ -15,8 +15,8 @@ use craft\events\SectionEvent; use craft\services\Fields; use nystudio107\codeeditor\autocompletes\SectionShorthandFieldsAutocomplete; +use nystudio107\codeeditor\base\Autocomplete; use nystudio107\codeeditor\base\Autocomplete as BaseAutoComplete; -use nystudio107\codeeditor\base\AutocompleteInterface; use nystudio107\codeeditor\CodeEditor; use nystudio107\codeeditor\events\RegisterCodeEditorAutocompletesEvent; use yii\base\Event; @@ -172,11 +172,11 @@ public function clearAutocompleteCache(string $autocompleteName = ''): void /** * Return the cache key to use for an Autocomplete's complete items * - * @param AutocompleteInterface $autocomplete + * @param Autocomplete $autocomplete * @param array $config * @return string */ - public function getAutocompleteCacheKey(AutocompleteInterface $autocomplete, array $config): string + public function getAutocompleteCacheKey(Autocomplete $autocomplete, array $config): string { return $this->cacheKeyPrefix . $autocomplete->name . md5(serialize($config)); } @@ -187,7 +187,7 @@ public function getAutocompleteCacheKey(AutocompleteInterface $autocomplete, arr /** * Returns all available autocompletes classes. * - * @return string[] The available autocompletes classes + * @return array The available autocompletes classes */ public function getAllAutocompleteGenerators(string $fieldType = CodeEditor::DEFAULT_FIELD_TYPE): array { From 492cf7c371afe40d1c4ea0b9f97837a04570ae2a Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Mon, 29 Jan 2024 22:10:47 -0500 Subject: [PATCH 08/10] fix: Use `getSections()` for Craft 3 & 4, and use `getEntries()` for Craft 5 --- .../SectionShorthandFieldsAutocomplete.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/autocompletes/SectionShorthandFieldsAutocomplete.php b/src/autocompletes/SectionShorthandFieldsAutocomplete.php index 9156f10..8d4aaa8 100644 --- a/src/autocompletes/SectionShorthandFieldsAutocomplete.php +++ b/src/autocompletes/SectionShorthandFieldsAutocomplete.php @@ -118,10 +118,17 @@ public function generateCompleteItems(): void return; } + $sections = null; + // getSections() is used for Craft 3 & 4 + if (method_exists(Craft::$app, 'getSections')) { + $sections = Craft::$app->getSections(); + } + // getEntries() is used for Craft 5 + if (method_exists(Craft::$app, 'getEntries')) { + $sections = Craft::$app->getEntries(); + } // Find the entry types used in the passed in sectionId - $sections = Craft::$app->getSections(); - $section = $sections->getSectionById($this->sectionId); - if ($section) { + if ($sections && $section = $sections->getSectionById($this->sectionId)) { $entryTypes = $section->getEntryTypes(); foreach ($entryTypes as $entryType) { // Add the native fields in From 0ed49caf0c5f93bbb27148899b4e93a7f429dcf3 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Mon, 29 Jan 2024 22:15:48 -0500 Subject: [PATCH 09/10] refactor: ECS code cleanup --- src/CodeEditor.php | 8 +-- .../codeeditor/CodeEditorAsset.php | 2 +- src/autocompletes/CraftApiAutocomplete.php | 4 +- .../SectionShorthandFieldsAutocomplete.php | 4 +- .../TwigLanguageAutocomplete.php | 14 ++--- src/base/Autocomplete.php | 6 +- src/base/ObjectParserAutocomplete.php | 15 ++--- src/config.php | 2 +- src/helpers/Config.php | 2 +- src/models/CompleteItem.php | 6 +- src/services/AutocompleteService.php | 8 +-- src/translations/en/codeeditor.php | 2 +- src/types/AutocompleteTypes.php | 4 +- src/types/CompleteItemKind.php | 56 +++++++++---------- src/types/CompletionItemInsertTextRule.php | 4 +- .../TwigObjectTemplateValidator.php | 2 +- src/validators/TwigTemplateValidator.php | 2 +- 17 files changed, 71 insertions(+), 70 deletions(-) diff --git a/src/CodeEditor.php b/src/CodeEditor.php index d89bd8a..b8ff39b 100755 --- a/src/CodeEditor.php +++ b/src/CodeEditor.php @@ -35,9 +35,9 @@ class CodeEditor extends Module implements BootstrapInterface // Constants // ========================================================================= - const ID = 'codeeditor'; + public const ID = 'codeeditor'; - const DEFAULT_FIELD_TYPE = 'CodeEditor'; + public const DEFAULT_FIELD_TYPE = 'CodeEditor'; // Public Static Properties // ========================================================================= @@ -135,14 +135,14 @@ public function registerComponents(): void public function registerEventHandlers(): void { // Base CP templates directory - Event::on(View::class, View::EVENT_REGISTER_CP_TEMPLATE_ROOTS, function (RegisterTemplateRootsEvent $e) { + Event::on(View::class, View::EVENT_REGISTER_CP_TEMPLATE_ROOTS, function(RegisterTemplateRootsEvent $e) { if (is_dir($baseDir = $this->getBasePath() . DIRECTORY_SEPARATOR . 'templates')) { $e->roots[$this->id] = $baseDir; } }); // Base Site templates directory if (self::$settings->allowTemplateAccess) { - Event::on(View::class, View::EVENT_REGISTER_SITE_TEMPLATE_ROOTS, function (RegisterTemplateRootsEvent $e) { + Event::on(View::class, View::EVENT_REGISTER_SITE_TEMPLATE_ROOTS, function(RegisterTemplateRootsEvent $e) { if (is_dir($baseDir = $this->getBasePath() . DIRECTORY_SEPARATOR . 'templates')) { $e->roots[$this->id] = $baseDir; } diff --git a/src/assetbundles/codeeditor/CodeEditorAsset.php b/src/assetbundles/codeeditor/CodeEditorAsset.php index 7ff2e2f..b270d9f 100644 --- a/src/assetbundles/codeeditor/CodeEditorAsset.php +++ b/src/assetbundles/codeeditor/CodeEditorAsset.php @@ -38,7 +38,7 @@ public function init() $this->js = [ 'js/runtime.js', 'js/vendors.js', - 'js/code-editor.js' + 'js/code-editor.js', ]; parent::init(); diff --git a/src/autocompletes/CraftApiAutocomplete.php b/src/autocompletes/CraftApiAutocomplete.php index 651933c..75b7212 100644 --- a/src/autocompletes/CraftApiAutocomplete.php +++ b/src/autocompletes/CraftApiAutocomplete.php @@ -27,9 +27,9 @@ class CraftApiAutocomplete extends ObjectParserAutocomplete // Constants // ========================================================================= - const ELEMENT_ROUTE_EXCLUDES = [ + public const ELEMENT_ROUTE_EXCLUDES = [ 'matrixblock', - 'globalset' + 'globalset', ]; // Public Properties diff --git a/src/autocompletes/SectionShorthandFieldsAutocomplete.php b/src/autocompletes/SectionShorthandFieldsAutocomplete.php index 8d4aaa8..5393391 100644 --- a/src/autocompletes/SectionShorthandFieldsAutocomplete.php +++ b/src/autocompletes/SectionShorthandFieldsAutocomplete.php @@ -40,7 +40,7 @@ class SectionShorthandFieldsAutocomplete extends ObjectParserAutocomplete 'type' => "the entry type", 'section' => "the entry’s section", 'author' => "the entry’s author", - ] + ], ]; // Public Properties @@ -133,7 +133,7 @@ public function generateCompleteItems(): void foreach ($entryTypes as $entryType) { // Add the native fields in if ($entryType->elementType) { - $element = new $entryType->elementType; + $element = new $entryType->elementType(); /* @var ElementInterface $element */ $this->parseObject('', $element, 0); $this->addMagicGetterProperties($element); diff --git a/src/autocompletes/TwigLanguageAutocomplete.php b/src/autocompletes/TwigLanguageAutocomplete.php index 0a06bfb..ee8d612 100644 --- a/src/autocompletes/TwigLanguageAutocomplete.php +++ b/src/autocompletes/TwigLanguageAutocomplete.php @@ -27,8 +27,8 @@ class TwigLanguageAutocomplete extends Autocomplete // Constants // ========================================================================= - const CRAFT_FILTER_DOCS_URL = 'https://craftcms.com/docs/4.x/dev/filters.html'; - const FILTER_DOCS = [ + public const CRAFT_FILTER_DOCS_URL = 'https://craftcms.com/docs/4.x/dev/filters.html'; + public const FILTER_DOCS = [ 'abs' => '[abs](https://twig.symfony.com/doc/3.x/filters/abs.html) | Returns the absolute value of a number.', 'address' => '[address](#address) | Formats an address.', 'append' => '[append](#append) | Appends HTML to the end of another element.', @@ -128,8 +128,8 @@ class TwigLanguageAutocomplete extends Autocomplete 'withoutKey' => '[withoutKey](#withoutkey) | Returns an array without the specified key.', ]; - const CRAFT_FUNCTION_DOCS_URL = 'https://craftcms.com/docs/4.x/dev/functions.html'; - const FUNCTION_DOCS = [ + public const CRAFT_FUNCTION_DOCS_URL = 'https://craftcms.com/docs/4.x/dev/functions.html'; + public const FUNCTION_DOCS = [ 'actionInput' => '[actionInput](#actioninput) | Outputs a hidden `action` input.', 'actionUrl' => '[actionUrl](#actionurl) | Generates a controller action URL.', 'alias' => '[alias](#alias) | Parses a string as an alias.', @@ -182,8 +182,8 @@ class TwigLanguageAutocomplete extends Autocomplete 'url' => '[url](#url) | Generates a URL.', ]; - const CRAFT_TAG_DOCS_URL = 'https://craftcms.com/docs/4.x/dev/tags.html'; - const TAG_DOCS = [ + public const CRAFT_TAG_DOCS_URL = 'https://craftcms.com/docs/4.x/dev/tags.html'; + public const TAG_DOCS = [ 'apply' => '[apply](https://twig.symfony.com/doc/3.x/tags/apply.html) | Applies Twig filters to the nested template code.', 'autoescape' => '[autoescape](https://twig.symfony.com/doc/3.x/tags/autoescape.html) | Controls the escaping strategy for the nested template code.', 'block' => '[block](https://twig.symfony.com/doc/3.x/tags/block.html) | Defines a template block.', @@ -227,7 +227,7 @@ class TwigLanguageAutocomplete extends Autocomplete 'with' => '[with](https://twig.symfony.com/doc/3.x/tags/with.html) | Creates a nested template scope.', ]; - const ADDITIONAL_TAGS = [ + public const ADDITIONAL_TAGS = [ 'else', 'elseif', 'endblock', diff --git a/src/base/Autocomplete.php b/src/base/Autocomplete.php index ec3be1a..341ce1b 100644 --- a/src/base/Autocomplete.php +++ b/src/base/Autocomplete.php @@ -27,7 +27,7 @@ abstract class Autocomplete extends Model implements AutocompleteInterface // Constants // ========================================================================= - const COMPLETION_KEY = '__completions'; + public const COMPLETION_KEY = '__completions'; // Public Properties // ========================================================================= @@ -88,9 +88,9 @@ public function addCompleteItem(CompleteItem $item, string $path = ''): void $path = $item->label; } ArrayHelper::setValue($this->completeItems, $path, [ - self::COMPLETION_KEY => array_filter($item->toArray(), static function ($v) { + self::COMPLETION_KEY => array_filter($item->toArray(), static function($v) { return !is_null($v); - }) + }), ]); } diff --git a/src/base/ObjectParserAutocomplete.php b/src/base/ObjectParserAutocomplete.php index 5457deb..9146fa5 100644 --- a/src/base/ObjectParserAutocomplete.php +++ b/src/base/ObjectParserAutocomplete.php @@ -17,6 +17,7 @@ use ReflectionClass; use ReflectionException; use ReflectionMethod; +use ReflectionNamedType; use ReflectionUnionType; use yii\base\Behavior; use yii\base\InvalidConfigException; @@ -32,25 +33,25 @@ abstract class ObjectParserAutocomplete extends Autocomplete implements ObjectPa // Constants // ========================================================================= - const EXCLUDED_PROPERTY_NAMES = [ + public const EXCLUDED_PROPERTY_NAMES = [ 'controller', 'Controller', 'CraftEdition', 'CraftSolo', 'CraftPro', ]; - const EXCLUDED_BEHAVIOR_NAMES = [ + public const EXCLUDED_BEHAVIOR_NAMES = [ 'fieldHandles', 'hasMethods', 'owner', ]; - const EXCLUDED_PROPERTY_REGEXES = [ + public const EXCLUDED_PROPERTY_REGEXES = [ '^_', ]; - const EXCLUDED_METHOD_REGEXES = [ + public const EXCLUDED_METHOD_REGEXES = [ '^_', ]; - const RECURSION_DEPTH_LIMIT = 10; + public const RECURSION_DEPTH_LIMIT = 10; // Public Properties // ========================================================================= @@ -323,7 +324,7 @@ protected function getMethodCompletion($object, DocBlockFactory $factory, string foreach ($params as $param) { if ($param->hasType()) { $reflectionType = $param->getType(); - if ($reflectionType instanceof \ReflectionUnionType) { + if ($reflectionType instanceof ReflectionUnionType) { $unionTypes = $reflectionType->getTypes(); $typeName = ''; foreach ($unionTypes as $unionType) { @@ -331,7 +332,7 @@ protected function getMethodCompletion($object, DocBlockFactory $factory, string } $typeName = trim($typeName, '|'); $paramList[] = $typeName . ': ' . '$' . $param->getName(); - } else if ($param->getType() instanceof \ReflectionNamedType) { + } elseif ($param->getType() instanceof ReflectionNamedType) { $paramList[] = $param->getType()->getName() . ': ' . '$' . $param->getName(); } } else { diff --git a/src/config.php b/src/config.php index 4d31005..60ae8bd 100644 --- a/src/config.php +++ b/src/config.php @@ -32,5 +32,5 @@ CraftApiAutocomplete::class, TwigLanguageAutocomplete::class, SectionShorthandFieldsAutocomplete::class, - ] + ], ]; diff --git a/src/helpers/Config.php b/src/helpers/Config.php index 93a867c..b2fde83 100644 --- a/src/helpers/Config.php +++ b/src/helpers/Config.php @@ -24,7 +24,7 @@ class Config // Constants // ========================================================================= - const PHP_SUFFIX = '.php'; + public const PHP_SUFFIX = '.php'; // Static Methods // ========================================================================= diff --git a/src/models/CompleteItem.php b/src/models/CompleteItem.php index 397cbd6..21906b3 100644 --- a/src/models/CompleteItem.php +++ b/src/models/CompleteItem.php @@ -304,7 +304,7 @@ public function defineRules(): array 'sortText', 'tags', ], - 'string' + 'string', ], [ [ @@ -313,7 +313,7 @@ public function defineRules(): array 'commitCharacters', 'range', ], - ArrayValidator::class + ArrayValidator::class, ], ['insertTextRules', 'integer', 'min' => 0, 'max' => 4], ['kind', 'integer', 'min' => 0, 'max' => 27], @@ -323,7 +323,7 @@ public function defineRules(): array 'insertText', 'kind', ], - 'required' + 'required', ], ]; } diff --git a/src/services/AutocompleteService.php b/src/services/AutocompleteService.php index f32c674..3ccfb2f 100755 --- a/src/services/AutocompleteService.php +++ b/src/services/AutocompleteService.php @@ -67,9 +67,9 @@ class AutocompleteService extends Component * * ``` */ - const EVENT_REGISTER_CODEEDITOR_AUTOCOMPLETES = 'registerCodeFieldAutocompletes'; + public const EVENT_REGISTER_CODEEDITOR_AUTOCOMPLETES = 'registerCodeFieldAutocompletes'; - const AUTOCOMPLETE_CACHE_TAG = 'CodeEditorAutocompleteTag'; + public const AUTOCOMPLETE_CACHE_TAG = 'CodeEditorAutocompleteTag'; // Public Properties // ========================================================================= @@ -98,7 +98,7 @@ public function init(): void $this->cacheDuration = 1; } // Invalidate any SectionShorthandFieldsAutocomplete caches whenever any field layout is edited - Event::on(Fields::class, Fields::EVENT_AFTER_SAVE_FIELD_LAYOUT, function (SectionEvent $e) { + Event::on(Fields::class, Fields::EVENT_AFTER_SAVE_FIELD_LAYOUT, function(SectionEvent $e) { $this->clearAutocompleteCache(SectionShorthandFieldsAutocomplete::class); }); } @@ -141,7 +141,7 @@ public function generateAutocompletes(string $fieldType = CodeEditor::DEFAULT_FI ], ]); // Get the autocompletes from the cache, or generate them if they aren't cached - $autocompleteItems[$name] = $cache->getOrSet($cacheKey, static function () use ($name, $autocomplete) { + $autocompleteItems[$name] = $cache->getOrSet($cacheKey, static function() use ($name, $autocomplete) { $autocomplete->generateCompleteItems(); return [ 'name' => $name, diff --git a/src/translations/en/codeeditor.php b/src/translations/en/codeeditor.php index 14f7c0d..9cb4d18 100644 --- a/src/translations/en/codeeditor.php +++ b/src/translations/en/codeeditor.php @@ -21,5 +21,5 @@ 'Twig Tag' => 'Twig Tag', 'Custom Field Shorthand' => 'Custom Field Shorthand', 'Code Editor field.' => 'Code Editor field.', - 'code is supported.' => 'code is supported.' + 'code is supported.' => 'code is supported.', ]; diff --git a/src/types/AutocompleteTypes.php b/src/types/AutocompleteTypes.php index f948919..903350d 100644 --- a/src/types/AutocompleteTypes.php +++ b/src/types/AutocompleteTypes.php @@ -21,6 +21,6 @@ abstract class AutocompleteTypes // ========================================================================= // Faux enum, No proper enums until PHP 8.1, and no constant visibility until PHP 7.1 - const TwigExpressionAutocomplete = 'TwigExpressionAutocomplete'; - const GeneralAutocomplete = 'GeneralAutocomplete'; + public const TwigExpressionAutocomplete = 'TwigExpressionAutocomplete'; + public const GeneralAutocomplete = 'GeneralAutocomplete'; } diff --git a/src/types/CompleteItemKind.php b/src/types/CompleteItemKind.php index 99a0187..8da40e6 100644 --- a/src/types/CompleteItemKind.php +++ b/src/types/CompleteItemKind.php @@ -23,32 +23,32 @@ abstract class CompleteItemKind // ========================================================================= // Faux enum, No proper enums until PHP 8.1, and no constant visibility until PHP 7.1 - const ClassKind = 5; - const ColorKind = 19; - const ConstantKind = 14; - const ConstructorKind = 2; - const CustomcolorKind = 22; - const EnumKind = 15; - const EnumMemberKind = 16; - const EventKind = 10; - const FieldKind = 3; - const FileKind = 20; - const FolderKind = 23; - const FunctionKind = 1; - const InterfaceKind = 7; - const IssueKind = 26; - const KeywordKind = 17; - const MethodKind = 0; - const ModuleKind = 8; - const OperatorKind = 11; - const PropertyKind = 9; - const ReferenceKind = 21; - const SnippetKind = 27; - const StructKind = 6; - const TextKind = 18; - const KindParameterKind = 24; - const UnitKind = 12; - const UserKind = 25; - const ValueKind = 13; - const VariableKind = 4; + public const ClassKind = 5; + public const ColorKind = 19; + public const ConstantKind = 14; + public const ConstructorKind = 2; + public const CustomcolorKind = 22; + public const EnumKind = 15; + public const EnumMemberKind = 16; + public const EventKind = 10; + public const FieldKind = 3; + public const FileKind = 20; + public const FolderKind = 23; + public const FunctionKind = 1; + public const InterfaceKind = 7; + public const IssueKind = 26; + public const KeywordKind = 17; + public const MethodKind = 0; + public const ModuleKind = 8; + public const OperatorKind = 11; + public const PropertyKind = 9; + public const ReferenceKind = 21; + public const SnippetKind = 27; + public const StructKind = 6; + public const TextKind = 18; + public const KindParameterKind = 24; + public const UnitKind = 12; + public const UserKind = 25; + public const ValueKind = 13; + public const VariableKind = 4; } diff --git a/src/types/CompletionItemInsertTextRule.php b/src/types/CompletionItemInsertTextRule.php index e68e256..4e476a7 100644 --- a/src/types/CompletionItemInsertTextRule.php +++ b/src/types/CompletionItemInsertTextRule.php @@ -23,6 +23,6 @@ abstract class CompletionItemInsertTextRule // ========================================================================= // Faux enum, No proper enums until PHP 8.1, and no constant visibility until PHP 7.1 - const InsertAsSnippet = 4; - const KeepWhitespace = 1; + public const InsertAsSnippet = 4; + public const KeepWhitespace = 1; } diff --git a/src/validators/TwigObjectTemplateValidator.php b/src/validators/TwigObjectTemplateValidator.php index f0855f9..06597b4 100644 --- a/src/validators/TwigObjectTemplateValidator.php +++ b/src/validators/TwigObjectTemplateValidator.php @@ -46,7 +46,7 @@ class TwigObjectTemplateValidator extends Validator * ); * ``` */ - const EVENT_REGISTER_TWIG_VALIDATOR_VARIABLES = 'registerTwigValidatorVariables'; + public const EVENT_REGISTER_TWIG_VALIDATOR_VARIABLES = 'registerTwigValidatorVariables'; /** * @var mixed The object that should be passed into `renderObjectTemplate()` during the template rendering diff --git a/src/validators/TwigTemplateValidator.php b/src/validators/TwigTemplateValidator.php index d6172ca..b0671f1 100644 --- a/src/validators/TwigTemplateValidator.php +++ b/src/validators/TwigTemplateValidator.php @@ -45,7 +45,7 @@ class TwigTemplateValidator extends Validator * ); * ``` */ - const EVENT_REGISTER_TWIG_VALIDATOR_VARIABLES = 'registerTwigValidatorVariables'; + public const EVENT_REGISTER_TWIG_VALIDATOR_VARIABLES = 'registerTwigValidatorVariables'; // Public Properties // ========================================================================= From f1b59855c0110ed03aa9b83784aecac8485a7c67 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Mon, 29 Jan 2024 22:16:04 -0500 Subject: [PATCH 10/10] chore: Version 1.0.17 --- CHANGELOG.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e9076f6..a77bde4 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,19 @@ All notable changes to this project will be documented in this file. +## 1.0.17 - 2024.01.29 +### Added +* Compatibility with Craft CMS 5 +* Add `phpstan` and `ecs` code linting +* Add `code-analysis.yaml` GitHub action + +### Changed +* PHPstan code cleanup +* ECS code cleanup + +### Fixed +* Use `getSections()` for Craft 3 & 4, and use `getEntries()` for Craft 5 + ## 1.0.16 - 2023.11.30 ### Changed * Update to `"monaco-editor": "^0.44.0"`