From 6054c175c96f3e2c2a9e362e995a106024d34fd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Sun, 11 Feb 2024 10:46:05 +0100 Subject: [PATCH] Modernize application --- .github/workflows/ci.yml | 16 ++++--- .php-cs-fixer.php | 2 +- CHANGELOG.md | 6 +++ README.md | 29 +++++++------ composer.json | 12 +++--- phpunit.xml.dist | 26 ++++++------ .../DependencyInjection/JoliTypoExtension.php | 5 +-- .../Bridge/Twig/JoliTypoExtension.php | 16 +++---- src/JoliTypo/Fixer.php | 8 ++-- src/JoliTypo/Fixer/Numeric.php | 2 - .../Tests/Bridge/app/AppController.php | 42 +++++++------------ tests/JoliTypo/Tests/Bridge/app/AppKernel.php | 2 +- .../Tests/Bridge/app/config/config.yml | 1 - .../Tests/Bridge/app/config/routing.yml | 4 +- 14 files changed, 81 insertions(+), 90 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a09e7a6..51f4a4a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,15 +8,19 @@ name: Continuous Integration jobs: ci: - name: Run the test suite + name: Test PHP ${{ matrix.php-version }} ${{ matrix.name }} runs-on: ubuntu-latest strategy: matrix: - php-versions: - - '7.4' - - '8.0' - - '8.1' + php-version: - '8.2' + - '8.3' + composer-flags: [''] + name: [''] + include: + - php-version: 8.1 + composer-flags: '--prefer-lowest' + name: '(prefer lowest dependencies)' steps: - uses: actions/checkout@v3 @@ -24,7 +28,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: '${{ matrix.php-versions }}' + php-version: '${{ matrix.php-version }}' - name: Validate composer.json and composer.lock run: composer validate --strict diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php index 49cbe4b..a0ac7cc 100644 --- a/.php-cs-fixer.php +++ b/.php-cs-fixer.php @@ -25,7 +25,7 @@ return (new PhpCsFixer\Config()) ->setRiskyAllowed(true) ->setRules([ - '@PHP74Migration' => true, + '@PHP81Migration' => true, '@PhpCsFixer' => true, '@Symfony' => true, '@Symfony:risky' => true, diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e23dfd..cfeeeab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,12 @@ CHANGELOG ### 1.5.0 (not released yet) ### +* Add support for PHP 8.3 +* Add support for Symfony 7 +* Drop support for PHP < 8.1 +* Drop support for Symfony < 5.4 and 6.1, 6.2, and 6.3 +* Drop support for Twig < 2 + ### 1.4.0 (2022-12-22) ### * Drop support for Symfony < 4.4 diff --git a/README.md b/README.md index e7f708b..6402b42 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ JoliTypo is a tool fixing [Microtypography](https://en.wikipedia.org/wiki/Microt ```php use JoliTypo\Fixer; -$fixer = new Fixer(array('Ellipsis', 'Dash', 'SmartQuotes', 'CurlyQuote', 'Hyphen')); +$fixer = new Fixer(['Ellipsis', 'Dash', 'SmartQuotes', 'CurlyQuote', 'Hyphen']); $fixedContent = $fixer->fix('

"Tell me Mr. Anderson... what good is a phone call... if you\'re unable to speak?" -- Agent Smith, Matrix.

'); ``` ```html @@ -37,7 +37,7 @@ Just tell the Fixer class [which Fixer](#available-fixers) you want to run on yo ```php use JoliTypo\Fixer; -$fixer = new Fixer(array("SmartQuotes", "FrenchNoBreakSpace")); +$fixer = new Fixer(["SmartQuotes", "FrenchNoBreakSpace"]); $fixer->setLocale('fr_FR'); $fixedContent = $fixer->fix('

Je suis "très content" de t\'avoir invité sur Jolicode.com !

'); ``` @@ -53,7 +53,7 @@ To fix non HTML content, use the `fixString()` method: ```php use JoliTypo\Fixer; -$fixer = new Fixer(array("Trademark", "SmartQuotes")); +$fixer = new Fixer(["Trademark", "SmartQuotes"]); $fixedContent = $fixer->fixString('Here is a "protip(c)"!'); // Here is a “protip©”! ``` @@ -169,7 +169,7 @@ en_GB ----- ```php -$fixer = new Fixer(array('Ellipsis', 'Dimension', 'Unit', 'Dash', 'SmartQuotes', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark')); +$fixer = new Fixer(['Ellipsis', 'Dimension', 'Unit', 'Dash', 'SmartQuotes', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark']); $fixer->setLocale('en_GB'); ``` @@ -179,7 +179,7 @@ fr_FR Those rules apply for most of the recommendations of "Abrégé du code typographique à l'usage de la presse", ISBN: 9782351130667. ```php -$fixer = new Fixer(array('Ellipsis', 'Dimension', 'Unit', 'Dash', 'SmartQuotes', 'FrenchNoBreakSpace', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark')); +$fixer = new Fixer(['Ellipsis', 'Dimension', 'Unit', 'Dash', 'SmartQuotes', 'FrenchNoBreakSpace', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark']); $fixer->setLocale('fr_FR'); ``` @@ -189,7 +189,7 @@ fr_CA Mostly the same as fr_FR, but the space before punctuation points is not mandatory. ```php -$fixer = new Fixer(array('Ellipsis', 'Dimension', 'Unit', 'Dash', 'SmartQuotes', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark')); +$fixer = new Fixer(['Ellipsis', 'Dimension', 'Unit', 'Dash', 'SmartQuotes', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark']); $fixer->setLocale('fr_CA'); ``` @@ -199,13 +199,12 @@ de_DE Mostly the same as en_GB, according to [Typefacts](http://typefacts.com/) and [Wikipedia](http://de.wikipedia.org/wiki/Typografie_f%C3%BCr_digitale_Texte). ```php -$fixer = new Fixer(array('Ellipsis', 'Dimension', 'Unit', 'Dash', 'SmartQuotes', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark')); +$fixer = new Fixer(['Ellipsis', 'Dimension', 'Unit', 'Dash', 'SmartQuotes', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'] ); $fixer->setLocale('de_DE'); ``` More to come (contributions welcome!). - Documentation ============= @@ -213,13 +212,13 @@ Default usage ------------- ```php -$fixer = new Fixer(array('Ellipsis', 'Dimension', 'Dash', 'SmartQuotes', 'CurlyQuote', 'Hyphen')); +$fixer = new Fixer(['Ellipsis', 'Dimension', 'Dash', 'SmartQuotes', 'CurlyQuote', 'Hyphen']); $fixedContent = $fixer->fix("

Some user contributed HTML which does not use proper glyphs.

"); -$fixer->setRules(array('CurlyQuote')); +$fixer->setRules(['CurlyQuote']); $fixedContent = $fixer->fix("

I'm only replacing single quotes.

"); -$fixer->setRules(array('Hyphen')); +$fixer->setRules(['Hyphen']); $fixer->setLocale('en_GB'); // I tell which locale to use for Hyphenation and SmartQuotes $fixedContent = $fixer->fix("

Very long words like Antidisestablishmentarianism.

"); ``` @@ -232,11 +231,11 @@ Then just give JoliTypo their fully qualified name, or even instance: ```php // by FQN -$fixer = new Fixer(array('Ellipsis', 'Acme\\YourOwn\\TypoFixer')); +$fixer = new Fixer(['Ellipsis', 'Acme\\YourOwn\\TypoFixer']); $fixedContent = $fixer->fix("

Content fixed by the 2 fixers.

"); // or instances, or both -$fixer = new Fixer(array('Ellipsis', 'Acme\\YourOwn\\TypoFixer', new Acme\\YourOwn\\PonyFixer("Some parameter"))); +$fixer = new Fixer(['Ellipsis', 'Acme\\YourOwn\\TypoFixer', new Acme\\YourOwn\\PonyFixer("Some parameter")]); $fixedContent = $fixer->fix("

Content fixed by the 3 fixers.

"); ``` @@ -246,8 +245,8 @@ Configure the protected tags Protected tags is a list of HTML tag names that the DOM parser must avoid. Nothing in those tags will be fixed. ```php -$fixer = new Fixer(array('Ellipsis')); -$fixer->setProtectedTags(array('pre', 'a')); +$fixer = new Fixer(['Ellipsis']); +$fixer->setProtectedTags(['pre', 'a']); $fixedContent = $fixer->fix("

Fixed...

Not fixed...

Fixed... Not Fixed....

"); ``` diff --git a/composer.json b/composer.json index 4ed2456..85323a5 100644 --- a/composer.json +++ b/composer.json @@ -13,17 +13,17 @@ } ], "require": { - "php": ">=7.4", + "php": ">=8.1", "ext-mbstring": "*", "lib-libxml": "*", "org_heigl/hyphenator": "^2.6 || ^3.0" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^3.3.2", - "symfony/phpunit-bridge": "^5.4.8 || ^6.0", - "symfony/framework-bundle": "^4.4 || ^5.0 || ^6.0", - "symfony/twig-bundle": "^4.4 || ^5.0 || ^6.0", - "symfony/yaml": "^4.4 || ^5.0 || ^6.0" + "friendsofphp/php-cs-fixer": "^3.49.0", + "symfony/phpunit-bridge": "^6.4.3", + "symfony/framework-bundle": "^5.4 || ^6.4 || 7.0", + "symfony/twig-bundle": "^5.4 || ^6.4 || 7.0", + "symfony/yaml": "^5.4 || ^6.4 || 7.0" }, "conflict": { "ext-apc": "3.1.11" diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 75afc07..38e7bb5 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,14 +1,16 @@ - - - - tests/JoliTypo/ - - - - - - src/JoliTypo/ - - + + + + + + + tests/JoliTypo/ + + + + + src/JoliTypo/ + + diff --git a/src/JoliTypo/Bridge/Symfony/DependencyInjection/JoliTypoExtension.php b/src/JoliTypo/Bridge/Symfony/DependencyInjection/JoliTypoExtension.php index 4d1870e..c1e2569 100644 --- a/src/JoliTypo/Bridge/Symfony/DependencyInjection/JoliTypoExtension.php +++ b/src/JoliTypo/Bridge/Symfony/DependencyInjection/JoliTypoExtension.php @@ -18,10 +18,7 @@ class JoliTypoExtension extends Extension { - /** - * {@inheritdoc} - */ - public function load(array $configs, ContainerBuilder $container) + public function load(array $configs, ContainerBuilder $container): void { $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); diff --git a/src/JoliTypo/Bridge/Twig/JoliTypoExtension.php b/src/JoliTypo/Bridge/Twig/JoliTypoExtension.php index 2ef361a..f5012f8 100644 --- a/src/JoliTypo/Bridge/Twig/JoliTypoExtension.php +++ b/src/JoliTypo/Bridge/Twig/JoliTypoExtension.php @@ -11,27 +11,27 @@ use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; use Twig\Extension\AbstractExtension; +use Twig\TwigFilter; +use Twig\TwigFunction; class JoliTypoExtension extends AbstractExtension { - private array $presets = []; - - public function __construct(array $presets) - { - $this->presets = $presets; + public function __construct( + private readonly array $presets = [], + ) { } public function getFunctions(): array { return [ - new \Twig\TwigFunction('jolitypo', [$this, 'translate']), + new TwigFunction('jolitypo', $this->translate(...)), ]; } public function getFilters(): array { return [ - new \Twig\TwigFilter('jolitypo', [$this, 'translate'], ['pre_escape' => 'html', 'is_safe' => ['html']]), + new TwigFilter('jolitypo', $this->translate(...), ['pre_escape' => 'html', 'is_safe' => ['html']]), ]; } @@ -41,7 +41,7 @@ public function getFilters(): array public function translate($text, $preset = 'default'): string { if (!isset($this->presets[$preset])) { - throw new InvalidConfigurationException(sprintf("There is no '%s' preset configured.", $preset)); + throw new InvalidConfigurationException(sprintf('There is no "%s" preset configured.', $preset)); } return $this->presets[$preset]->fix($text); diff --git a/src/JoliTypo/Fixer.php b/src/JoliTypo/Fixer.php index 7dd34ac..583113a 100644 --- a/src/JoliTypo/Fixer.php +++ b/src/JoliTypo/Fixer.php @@ -190,7 +190,7 @@ private function compileRules(array $rules): void foreach ($rules as $rule) { if (\is_object($rule)) { $fixer = $rule; - $className = \get_class($rule); + $className = $rule::class; } else { $className = class_exists($rule) ? $rule : (class_exists( 'JoliTypo\\Fixer\\' . $rule @@ -275,7 +275,7 @@ private function doFix(\DOMText $childNode, \DOMNode $node, \DOMDocument $dom): } /** - * @throws Exception\InvalidMarkupException + * @throws InvalidMarkupException */ private function loadDOMDocument($content): \DOMDocument { @@ -311,8 +311,8 @@ private function fixContentEncoding($content): string { if (!empty($content)) { // Little hack to force UTF-8 - if (false === strpos($content, '' : ''; diff --git a/src/JoliTypo/Fixer/Numeric.php b/src/JoliTypo/Fixer/Numeric.php index f1cd107..69e355b 100644 --- a/src/JoliTypo/Fixer/Numeric.php +++ b/src/JoliTypo/Fixer/Numeric.php @@ -10,8 +10,6 @@ namespace JoliTypo\Fixer; /** - * {@inheritdoc} - * * @deprecated Numeric should not be used (reserved keyword in PHP7) */ class Numeric extends Unit diff --git a/tests/JoliTypo/Tests/Bridge/app/AppController.php b/tests/JoliTypo/Tests/Bridge/app/AppController.php index a4db577..e5d3697 100644 --- a/tests/JoliTypo/Tests/Bridge/app/AppController.php +++ b/tests/JoliTypo/Tests/Bridge/app/AppController.php @@ -14,38 +14,24 @@ class AppController { - private Environment $twig; - - public function __construct(Environment $twig) - { - $this->twig = $twig; + public function __construct( + private Environment $twig, + ) { } public function fixAction(): Response { - if (1 === $this->twig::MAJOR_VERSION) { - $template = $this->twig->createTemplate( - <<<'TWIG' -

Raw content: People's.

- -

{{ "Fixed content: People's."|jolitypo('en') }}

- TWIG - ); - - $content = $template->render([]); - } else { - $template = $this->twig->createTemplate( - <<<'TWIG' -

Raw content: People's.

- - {% apply jolitypo('en') %} -

Fixed content: People's.

- {% endapply %} - TWIG - ); - - $content = $this->twig->render($template); - } + $template = $this->twig->createTemplate( + <<<'TWIG' +

Raw content: People's.

+ + {% apply jolitypo('en') %} +

Fixed content: People's.

+ {% endapply %} + TWIG + ); + + $content = $this->twig->render($template); return new Response($content); } diff --git a/tests/JoliTypo/Tests/Bridge/app/AppKernel.php b/tests/JoliTypo/Tests/Bridge/app/AppKernel.php index a84171f..7a1a3e4 100644 --- a/tests/JoliTypo/Tests/Bridge/app/AppKernel.php +++ b/tests/JoliTypo/Tests/Bridge/app/AppKernel.php @@ -28,7 +28,7 @@ public function registerBundles(): iterable ]; } - public function registerContainerConfiguration(LoaderInterface $loader) + public function registerContainerConfiguration(LoaderInterface $loader): void { $loader->load(__DIR__ . '/config/config.yml'); diff --git a/tests/JoliTypo/Tests/Bridge/app/config/config.yml b/tests/JoliTypo/Tests/Bridge/app/config/config.yml index 3f7f31f..db2aafe 100644 --- a/tests/JoliTypo/Tests/Bridge/app/config/config.yml +++ b/tests/JoliTypo/Tests/Bridge/app/config/config.yml @@ -21,6 +21,5 @@ joli_typo: services: JoliTypo\Tests\Bridge\app\AppController: - class: \JoliTypo\Tests\Bridge\app\AppController public: true arguments: ['@twig'] diff --git a/tests/JoliTypo/Tests/Bridge/app/config/routing.yml b/tests/JoliTypo/Tests/Bridge/app/config/routing.yml index e29937d..f0b37e4 100644 --- a/tests/JoliTypo/Tests/Bridge/app/config/routing.yml +++ b/tests/JoliTypo/Tests/Bridge/app/config/routing.yml @@ -1,3 +1,3 @@ fix: - path: /fix - defaults: { _controller: JoliTypo\Tests\Bridge\app\AppController::fixAction } + path: /fix + controller: JoliTypo\Tests\Bridge\app\AppController::fixAction