Skip to content

Commit

Permalink
Merge pull request #102 from jolicode/php
Browse files Browse the repository at this point in the history
Modernize application
  • Loading branch information
lyrixx committed Feb 11, 2024
2 parents 20080ef + 6054c17 commit 449ff74
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 90 deletions.
16 changes: 10 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,27 @@ 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

- 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
Expand Down
2 changes: 1 addition & 1 deletion .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
return (new PhpCsFixer\Config())
->setRiskyAllowed(true)
->setRules([
'@PHP74Migration' => true,
'@PHP81Migration' => true,
'@PhpCsFixer' => true,
'@Symfony' => true,
'@Symfony:risky' => true,
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 14 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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('<p>"Tell me Mr. Anderson... what good is a phone call... if you\'re unable to speak?" -- Agent Smith, <em>Matrix</em>.</p>');
```
```html
Expand Down Expand Up @@ -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('<p>Je suis "très content" de t\'avoir invité sur <a href="http://jolicode.com/">Jolicode.com</a> !</p>');
```
Expand All @@ -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©”!
```

Expand Down Expand Up @@ -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');
```

Expand All @@ -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');
```

Expand All @@ -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');
```

Expand All @@ -199,27 +199,26 @@ 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
=============

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("<p>Some user contributed HTML which does not use proper glyphs.</p>");

$fixer->setRules(array('CurlyQuote'));
$fixer->setRules(['CurlyQuote']);
$fixedContent = $fixer->fix("<p>I'm only replacing single quotes.</p>");

$fixer->setRules(array('Hyphen'));
$fixer->setRules(['Hyphen']);
$fixer->setLocale('en_GB'); // I tell which locale to use for Hyphenation and SmartQuotes
$fixedContent = $fixer->fix("<p>Very long words like Antidisestablishmentarianism.</p>");
```
Expand All @@ -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("<p>Content fixed by the 2 fixers.</p>");

// 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("<p>Content fixed by the 3 fixers.</p>");
```

Expand All @@ -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("<p>Fixed...</p> <pre>Not fixed...</pre> <p>Fixed... <a>Not Fixed...</a>.</p>");
```

Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
26 changes: 14 additions & 12 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="JoliTypo Test Suite">
<directory>tests/JoliTypo/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">src/JoliTypo/</directory>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<php>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0"/>
</php>
<testsuites>
<testsuite name="JoliTypo Test Suite">
<directory>tests/JoliTypo/</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">src/JoliTypo/</directory>
</include>
</coverage>
</phpunit>
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 8 additions & 8 deletions src/JoliTypo/Bridge/Twig/JoliTypoExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']]),
];
}

Expand All @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/JoliTypo/Fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -275,7 +275,7 @@ private function doFix(\DOMText $childNode, \DOMNode $node, \DOMDocument $dom):
}

/**
* @throws Exception\InvalidMarkupException
* @throws InvalidMarkupException
*/
private function loadDOMDocument($content): \DOMDocument
{
Expand Down Expand Up @@ -311,8 +311,8 @@ private function fixContentEncoding($content): string
{
if (!empty($content)) {
// Little hack to force UTF-8
if (false === strpos($content, '<?xml encoding')) {
$hack = false === strpos(
if (!str_contains($content, '<?xml encoding')) {
$hack = !str_contains(
$content,
'<body'
) ? '<?xml encoding="UTF-8"><body>' : '<?xml encoding="UTF-8">';
Expand Down
2 changes: 0 additions & 2 deletions src/JoliTypo/Fixer/Numeric.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
namespace JoliTypo\Fixer;

/**
* {@inheritdoc}
*
* @deprecated Numeric should not be used (reserved keyword in PHP7)
*/
class Numeric extends Unit
Expand Down
42 changes: 14 additions & 28 deletions tests/JoliTypo/Tests/Bridge/app/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
<p>Raw content: People's.</p>
<p>{{ "Fixed content: People's."|jolitypo('en') }}</p>
TWIG
);

$content = $template->render([]);
} else {
$template = $this->twig->createTemplate(
<<<'TWIG'
<p>Raw content: People's.</p>
{% apply jolitypo('en') %}
<p>Fixed content: People's.</p>
{% endapply %}
TWIG
);

$content = $this->twig->render($template);
}
$template = $this->twig->createTemplate(
<<<'TWIG'
<p>Raw content: People's.</p>
{% apply jolitypo('en') %}
<p>Fixed content: People's.</p>
{% endapply %}
TWIG
);

$content = $this->twig->render($template);

return new Response($content);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/JoliTypo/Tests/Bridge/app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
1 change: 0 additions & 1 deletion tests/JoliTypo/Tests/Bridge/app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,5 @@ joli_typo:

services:
JoliTypo\Tests\Bridge\app\AppController:
class: \JoliTypo\Tests\Bridge\app\AppController
public: true
arguments: ['@twig']
4 changes: 2 additions & 2 deletions tests/JoliTypo/Tests/Bridge/app/config/routing.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fix:
path: /fix
defaults: { _controller: JoliTypo\Tests\Bridge\app\AppController::fixAction }
path: /fix
controller: JoliTypo\Tests\Bridge\app\AppController::fixAction

0 comments on commit 449ff74

Please sign in to comment.