From f4960490719b0848007c753a94a7989f425a3f63 Mon Sep 17 00:00:00 2001 From: Laurent Muller Date: Mon, 22 Apr 2024 09:00:33 +0200 Subject: [PATCH 1/2] Removed "orklah/psalm-*" plugins. --- CHANGELOG.md | 2 + composer.json | 3 - composer.lock | 187 ++++---------------------------------------------- 3 files changed, 15 insertions(+), 177 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c2a5ca..0eaff72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## [Unreleased] +- Removed `orklah/psalm-*` plugins. + ## [2.20.11] - 2024-04-18 - Modified the constructor. diff --git a/composer.json b/composer.json index ca3e4df..1cc3c3e 100644 --- a/composer.json +++ b/composer.json @@ -31,9 +31,6 @@ "phpstan/phpstan-deprecation-rules": "^1.0", "phpstan/phpstan-phpunit": "^1.0", "slam/phpstan-extensions": "^6.0", - "orklah/psalm-insane-comparison": "^2.0", - "orklah/psalm-not-empty": "^1.0", - "orklah/psalm-strict-equality": "^2.0", "spaze/phpstan-disallowed-calls": "^2.0", "phpstan/phpstan": "^1.0", "phpstan/phpstan-strict-rules": "^1.5", diff --git a/composer.lock b/composer.lock index c7af917..50b3d90 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "dc8fa8f3e52c51098dabbceb2fb77c47", + "content-hash": "482891e019c346e98b98582b01824627", "packages": [ { "name": "laminas/laminas-json", @@ -1555,16 +1555,16 @@ }, { "name": "twig/twig", - "version": "v3.9.2", + "version": "v3.9.3", "source": { "type": "git", "url": "https://github.com/twigphp/Twig.git", - "reference": "856cb5a6cfd6f3e4dc1f6c9a8f54e259503f7cf3" + "reference": "a842d75fed59cdbcbd3a3ad7fb9eb768fc350d58" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/twigphp/Twig/zipball/856cb5a6cfd6f3e4dc1f6c9a8f54e259503f7cf3", - "reference": "856cb5a6cfd6f3e4dc1f6c9a8f54e259503f7cf3", + "url": "https://api.github.com/repos/twigphp/Twig/zipball/a842d75fed59cdbcbd3a3ad7fb9eb768fc350d58", + "reference": "a842d75fed59cdbcbd3a3ad7fb9eb768fc350d58", "shasum": "" }, "require": { @@ -1618,7 +1618,7 @@ ], "support": { "issues": "https://github.com/twigphp/Twig/issues", - "source": "https://github.com/twigphp/Twig/tree/v3.9.2" + "source": "https://github.com/twigphp/Twig/tree/v3.9.3" }, "funding": [ { @@ -1630,7 +1630,7 @@ "type": "tidelift" } ], - "time": "2024-04-17T14:16:25+00:00" + "time": "2024-04-18T11:59:33+00:00" } ], "packages-dev": [ @@ -2590,167 +2590,6 @@ }, "time": "2024-03-17T08:10:35+00:00" }, - { - "name": "orklah/psalm-insane-comparison", - "version": "v2.3.0", - "source": { - "type": "git", - "url": "https://github.com/orklah/psalm-insane-comparison.git", - "reference": "d9e608e787e5ae1c994ae07095eea26029d6d0bd" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/orklah/psalm-insane-comparison/zipball/d9e608e787e5ae1c994ae07095eea26029d6d0bd", - "reference": "d9e608e787e5ae1c994ae07095eea26029d6d0bd", - "shasum": "" - }, - "require": { - "ext-simplexml": "*", - "php": "^7.3|^8.0", - "vimeo/psalm": "^4|^5|dev-master" - }, - "require-dev": { - "nikic/php-parser": "^4.0|^5" - }, - "type": "psalm-plugin", - "extra": { - "psalm": { - "pluginClass": "Orklah\\PsalmInsaneComparison\\Plugin" - } - }, - "autoload": { - "psr-4": { - "Orklah\\PsalmInsaneComparison\\": [ - "." - ], - "Orklah\\PsalmInsaneComparison\\Hooks\\": [ - "hooks" - ] - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "orklah" - } - ], - "description": "Detects possible insane comparison (\"string\" == 0) to help migrate to PHP8", - "support": { - "issues": "https://github.com/orklah/psalm-insane-comparison/issues", - "source": "https://github.com/orklah/psalm-insane-comparison/tree/v2.3.0" - }, - "time": "2024-03-20T21:49:45+00:00" - }, - { - "name": "orklah/psalm-not-empty", - "version": "v1.1.0", - "source": { - "type": "git", - "url": "https://github.com/orklah/psalm-not-empty.git", - "reference": "81c24879a8563be9b0691605fc923cb5986d1527" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/orklah/psalm-not-empty/zipball/81c24879a8563be9b0691605fc923cb5986d1527", - "reference": "81c24879a8563be9b0691605fc923cb5986d1527", - "shasum": "" - }, - "require": { - "php": "^7.3|^8.0", - "vimeo/psalm": "^5" - }, - "require-dev": { - "nikic/php-parser": "^4.0", - "phpunit/phpunit": "^9.5", - "psalm/plugin-phpunit": "^0.18.0" - }, - "type": "psalm-plugin", - "extra": { - "psalm": { - "pluginClass": "Orklah\\NotEmpty\\Plugin" - } - }, - "autoload": { - "psr-4": { - "Orklah\\NotEmpty\\": [ - "./src" - ] - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "orklah" - } - ], - "description": "Automatically change empty() into a more explicit expression", - "support": { - "issues": "https://github.com/orklah/psalm-not-empty/issues", - "source": "https://github.com/orklah/psalm-not-empty/tree/v1.1.0" - }, - "time": "2022-12-02T21:52:27+00:00" - }, - { - "name": "orklah/psalm-strict-equality", - "version": "v2.1.0", - "source": { - "type": "git", - "url": "https://github.com/orklah/psalm-strict-equality.git", - "reference": "f733a5d7f575632404f0e002fdf8dcad8c5dd39a" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/orklah/psalm-strict-equality/zipball/f733a5d7f575632404f0e002fdf8dcad8c5dd39a", - "reference": "f733a5d7f575632404f0e002fdf8dcad8c5dd39a", - "shasum": "" - }, - "require": { - "nikic/php-parser": "^4.0", - "php": "^7.3|^8.0", - "vimeo/psalm": "^4.6.1|^5.0" - }, - "require-dev": { - "phpunit/phpunit": "^9.5", - "psalm/plugin-phpunit": "^0.15.0" - }, - "type": "psalm-plugin", - "extra": { - "psalm": { - "pluginClass": "Orklah\\StrictEquality\\Plugin" - } - }, - "autoload": { - "psr-4": { - "Orklah\\StrictEquality\\": [ - "./src" - ], - "Orklah\\StrictEquality\\Tests\\": [ - "./tests" - ] - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "orklah" - } - ], - "description": "Automatically change == into === when safe ", - "support": { - "issues": "https://github.com/orklah/psalm-strict-equality/issues", - "source": "https://github.com/orklah/psalm-strict-equality/tree/v2.1.0" - }, - "time": "2022-12-05T19:50:08+00:00" - }, { "name": "phar-io/manifest", "version": "2.0.4", @@ -3295,16 +3134,16 @@ }, { "name": "phpstan/phpstan-strict-rules", - "version": "1.5.3", + "version": "1.5.5", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan-strict-rules.git", - "reference": "568210bd301f94a0d4b1e5a0808c374c1b9cf11b" + "reference": "2e193a07651a6f4be3baa44ddb21d822681f5918" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan-strict-rules/zipball/568210bd301f94a0d4b1e5a0808c374c1b9cf11b", - "reference": "568210bd301f94a0d4b1e5a0808c374c1b9cf11b", + "url": "https://api.github.com/repos/phpstan/phpstan-strict-rules/zipball/2e193a07651a6f4be3baa44ddb21d822681f5918", + "reference": "2e193a07651a6f4be3baa44ddb21d822681f5918", "shasum": "" }, "require": { @@ -3338,9 +3177,9 @@ "description": "Extra strict and opinionated rules for PHPStan", "support": { "issues": "https://github.com/phpstan/phpstan-strict-rules/issues", - "source": "https://github.com/phpstan/phpstan-strict-rules/tree/1.5.3" + "source": "https://github.com/phpstan/phpstan-strict-rules/tree/1.5.5" }, - "time": "2024-04-06T07:43:25+00:00" + "time": "2024-04-19T15:12:26+00:00" }, { "name": "phpunit/php-code-coverage", From 343117e47f153884e935ca644b1f00d68a945dfa Mon Sep 17 00:00:00 2001 From: Laurent Muller Date: Mon, 22 Apr 2024 10:50:30 +0200 Subject: [PATCH 2/2] Version 2.20.12. - Replaced engine string values by "Engine" enumeration. --- CHANGELOG.md | 4 ++ src/Highcharts/AbstractChart.php | 26 ++++--------- src/Highcharts/ChartInterface.php | 10 +---- src/Highcharts/Engine.php | 34 +++++++++++++++++ src/Twig/HighchartsExtension.php | 22 ++++++++++- tests/AbstractChartTestCase.php | 11 +++--- tests/Highcharts/EngineTest.php | 60 ++++++++++++++++++++++++++++++ tests/Highcharts/HighchartTest.php | 7 ++-- tests/Twig/TwigTest.php | 25 +++++++++++++ 9 files changed, 161 insertions(+), 38 deletions(-) create mode 100644 src/Highcharts/Engine.php create mode 100644 tests/Highcharts/EngineTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 0eaff72..391407a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,10 @@ ## [Unreleased] +## [2.20.12] - 2024-04-22 + - Removed `orklah/psalm-*` plugins. +- Replaced engine string values by `Engine` enumeration. ## [2.20.11] - 2024-04-18 @@ -140,6 +143,7 @@ as possible. See [Security Advisory: ZF2014-01](https://framework.zend.com/secur - Initial release [Unreleased]: https://github.com/laurentmuller/HighchartsBundle/compare/1.7...HEAD +[2.20.12]: https://github.com/laurentmuller/HighchartsBundle/compare/2.20.11...2.20.12 [2.20.11]: https://github.com/laurentmuller/HighchartsBundle/compare/2.20.10...2.20.11 [2.20.10]: https://github.com/laurentmuller/HighchartsBundle/compare/2.20.9...2.20.10 [2.20.9]: https://github.com/laurentmuller/HighchartsBundle/compare/2.20.8...2.20.9 diff --git a/src/Highcharts/AbstractChart.php b/src/Highcharts/AbstractChart.php index e97897b..43a099a 100644 --- a/src/Highcharts/AbstractChart.php +++ b/src/Highcharts/AbstractChart.php @@ -94,10 +94,7 @@ public static function createExpression(string $expression): Expr return new Expr($expression); } - /** - * @psalm-param ChartInterface::ENGINE_* $engine - */ - public function render(string $engine = self::ENGINE_JQUERY): string + public function render(Engine $engine = Engine::JQUERY): string { $chartJS = ''; $this->renderChartStart($chartJS, $engine); @@ -174,13 +171,10 @@ protected function renderChartCommon(string &$chartJS): void $this->renderAccessibility($chartJS); } - /** - * @psalm-param ChartInterface::ENGINE_* $engine - */ - protected function renderChartEnd(string &$chartJS, string $engine): void + protected function renderChartEnd(string &$chartJS, Engine $engine): void { $chartJS = \rtrim($chartJS, self::END_LINE) . self::NEW_LINE . self::HALF_SPACE . '});' . self::NEW_LINE; - if (self::ENGINE_NONE !== $engine) { + if (Engine::NONE !== $engine) { $chartJS .= '});' . self::NEW_LINE; } } @@ -189,10 +183,7 @@ protected function renderChartOptions(string &$chartJS): void { } - /** - * @psalm-param ChartInterface::ENGINE_* $engine - */ - protected function renderChartStart(string &$chartJS, string $engine): void + protected function renderChartStart(string &$chartJS, Engine $engine): void { $this->renderEngine($chartJS, $engine); $this->renderOptions($chartJS); @@ -209,14 +200,11 @@ protected function renderCredits(string &$chartJS): void $chartJS .= $this->jsonEncode($this->credits); } - /** - * @psalm-param ChartInterface::ENGINE_* $engine - */ - protected function renderEngine(string &$chartJS, string $engine): void + protected function renderEngine(string &$chartJS, Engine $engine): void { $chartJS .= match ($engine) { - self::ENGINE_MOOTOOLS => 'window.addEvent(\'domready\', function () {', - self::ENGINE_JQUERY => '$(function () {', + Engine::MOOTOOLS => 'window.addEvent(\'domready\', function () {', + Engine::JQUERY => '$(function () {', default => '', }; } diff --git a/src/Highcharts/ChartInterface.php b/src/Highcharts/ChartInterface.php index 8db4f38..ec34e99 100644 --- a/src/Highcharts/ChartInterface.php +++ b/src/Highcharts/ChartInterface.php @@ -17,16 +17,8 @@ */ interface ChartInterface { - public const ENGINE_JQUERY = 'jquery'; - - public const ENGINE_MOOTOOLS = 'mootools'; - - public const ENGINE_NONE = ''; - /** * Render this chart for the given engine. - * - * @psalm-param self::ENGINE_* $engine */ - public function render(string $engine): string; + public function render(Engine $engine): string; } diff --git a/src/Highcharts/Engine.php b/src/Highcharts/Engine.php new file mode 100644 index 0000000..de6515b --- /dev/null +++ b/src/Highcharts/Engine.php @@ -0,0 +1,34 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace HighchartsBundle\Highcharts; + +/** + * The render engine enumeration. + * + * @see ChartInterface::render() + */ +enum Engine: string +{ + /** + * The JQuery engine. + */ + case JQUERY = 'jquery'; + /** + * The MooTools engine. + */ + case MOOTOOLS = 'mootools'; + /** + * No engine. + */ + case NONE = ''; +} diff --git a/src/Twig/HighchartsExtension.php b/src/Twig/HighchartsExtension.php index 519a69f..d2622ab 100644 --- a/src/Twig/HighchartsExtension.php +++ b/src/Twig/HighchartsExtension.php @@ -13,6 +13,8 @@ namespace HighchartsBundle\Twig; use HighchartsBundle\Highcharts\ChartInterface; +use HighchartsBundle\Highcharts\Engine; +use Twig\Error\SyntaxError; use Twig\Extension\AbstractExtension; use Twig\TwigFunction; @@ -24,10 +26,14 @@ class HighchartsExtension extends AbstractExtension /** * Render the given chart with the given engine. * - * @psalm-param ChartInterface::ENGINE_* $engine + * @throws SyntaxError if the engine is a string and the corresponding enumeration cannot be found */ - public function chart(ChartInterface $chart, string $engine = ChartInterface::ENGINE_JQUERY): string + public function chart(ChartInterface $chart, Engine|string $engine = Engine::JQUERY): string { + if (\is_string($engine)) { + $engine = $this->parseEngine($engine); + } + return $chart->render($engine); } @@ -37,4 +43,16 @@ public function getFunctions(): array new TwigFunction('chart', $this->chart(...), ['is_safe' => ['html']]), ]; } + + /** + * @throws SyntaxError + */ + private function parseEngine(string $engine): Engine + { + try { + return Engine::from($engine); + } catch (\ValueError $e) { + throw new SyntaxError("Invalid chart engine: \"$engine\".", previous: $e); + } + } } diff --git a/tests/AbstractChartTestCase.php b/tests/AbstractChartTestCase.php index 79dbe59..75768d4 100644 --- a/tests/AbstractChartTestCase.php +++ b/tests/AbstractChartTestCase.php @@ -13,15 +13,16 @@ namespace HighchartsBundle\Tests; use HighchartsBundle\Highcharts\ChartInterface; +use HighchartsBundle\Highcharts\Engine; use PHPUnit\Framework\TestCase; abstract class AbstractChartTestCase extends TestCase { - /** - * @psalm-param ChartInterface::ENGINE_* $engine - */ - protected static function assertChartMatchesRegularExpression(ChartInterface $chart, string $regex, string $engine = 'jquery'): void - { + protected static function assertChartMatchesRegularExpression( + ChartInterface $chart, + string $regex, + Engine $engine = Engine::JQUERY + ): void { $result = $chart->render($engine); self::assertMatchesRegularExpression($regex, $result); } diff --git a/tests/Highcharts/EngineTest.php b/tests/Highcharts/EngineTest.php new file mode 100644 index 0000000..ae29c87 --- /dev/null +++ b/tests/Highcharts/EngineTest.php @@ -0,0 +1,60 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace HighchartsBundle\Tests\Highcharts; + +use HighchartsBundle\Highcharts\Engine; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\TestCase; + +class EngineTest extends TestCase +{ + public static function getInvalidValues(): \Generator + { + yield ['fake']; + } + + public static function getValues(): \Generator + { + yield [Engine::JQUERY, 'jquery']; + yield [Engine::MOOTOOLS, 'mootools']; + yield [Engine::NONE, '']; + } + + public function testCount(): void + { + $values = Engine::cases(); + self::assertCount(3, $values); + } + + #[DataProvider('getValues')] + public function testFrom(Engine $engine, string $value): void + { + $actual = Engine::from($value); + self::assertSame($actual, $engine); + } + + #[DataProvider('getInvalidValues')] + public function testInvalidValue(string $value): void + { + self::expectException(\ValueError::class); + Engine::from($value); + self::fail('A value error must be raised.'); + } + + #[DataProvider('getValues')] + public function testValue(Engine $engine, string $expected): void + { + $actual = $engine->value; + self::assertSame($expected, $actual); + } +} diff --git a/tests/Highcharts/HighchartTest.php b/tests/Highcharts/HighchartTest.php index c74e02c..d964de9 100644 --- a/tests/Highcharts/HighchartTest.php +++ b/tests/Highcharts/HighchartTest.php @@ -12,6 +12,7 @@ namespace HighchartsBundle\Tests\Highcharts; +use HighchartsBundle\Highcharts\Engine; use HighchartsBundle\Highcharts\Highchart; use HighchartsBundle\Tests\AbstractChartTestCase; @@ -37,17 +38,17 @@ public function testMooTools(): void { $chart = new Highchart(); $regex = '/window.addEvent\(\'domready\', function\s?\(\)\s?\{\r?\n?\s*const chart = new Highcharts.Chart\(\{\n?\r?\s*\}\);\n?\r?\s*\}\);/'; - self::assertChartMatchesRegularExpression($chart, $regex, 'mootools'); + self::assertChartMatchesRegularExpression($chart, $regex, Engine::MOOTOOLS); } /** - * Render chart without library wrapper. + * Render chart without a library wrapper. */ public function testNoEngine(): void { $chart = new Highchart(); $regex = '/const chart = new Highcharts.Chart\(\{\n?\r?\s*\}\);/'; - self::assertChartMatchesRegularExpression($chart, $regex, ''); + self::assertChartMatchesRegularExpression($chart, $regex, Engine::NONE); } /** diff --git a/tests/Twig/TwigTest.php b/tests/Twig/TwigTest.php index ba3cb82..886d7c2 100644 --- a/tests/Twig/TwigTest.php +++ b/tests/Twig/TwigTest.php @@ -12,17 +12,32 @@ namespace HighchartsBundle\Tests\Twig; +use HighchartsBundle\Highcharts\Engine; use HighchartsBundle\Highcharts\Highchart; use HighchartsBundle\Twig\HighchartsExtension; use PHPUnit\Framework\TestCase; +use Twig\Error\SyntaxError; /** * This class hold Unit Tests for the Twig extension. */ class TwigTest extends TestCase { + /** + * @throws SyntaxError + */ + public function testInvalidEngine(): void + { + $this->expectException(SyntaxError::class); + $chart = new Highchart(); + $extension = new HighchartsExtension(); + $extension->chart($chart, 'fake'); + } + /** * Chart rendering using the twig extension. + * + * @throws SyntaxError */ public function testTwigExtension(): void { @@ -36,6 +51,11 @@ public function testTwigExtension(): void ); // render with jquery explicitly + $actual = $extension->chart($chart, Engine::JQUERY); + self::assertMatchesRegularExpression( + '/\$\(function\s?\(\)\s?\{\n?\r?\s*const chart = new Highcharts.Chart\(\{\n?\r?\s*\}\);\n?\r?\s*\}\);/', + $actual + ); $actual = $extension->chart($chart, 'jquery'); self::assertMatchesRegularExpression( '/\$\(function\s?\(\)\s?\{\n?\r?\s*const chart = new Highcharts.Chart\(\{\n?\r?\s*\}\);\n?\r?\s*\}\);/', @@ -43,6 +63,11 @@ public function testTwigExtension(): void ); // render with mootools + $actual = $extension->chart($chart, Engine::MOOTOOLS); + self::assertMatchesRegularExpression( + '/window.addEvent\(\'domready\', function\s?\(\)\s?\{\r?\n?\s*const chart = new Highcharts.Chart\(\{\n?\r?\s*\}\);\n?\r?\s*\}\);/', + $actual + ); $actual = $extension->chart($chart, 'mootools'); self::assertMatchesRegularExpression( '/window.addEvent\(\'domready\', function\s?\(\)\s?\{\r?\n?\s*const chart = new Highcharts.Chart\(\{\n?\r?\s*\}\);\n?\r?\s*\}\);/',