Skip to content

Commit

Permalink
Update php-cs-fixer and its config and fix the codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrixx committed Dec 5, 2021
1 parent b4def6d commit 4f6d740
Show file tree
Hide file tree
Showing 34 changed files with 394 additions and 382 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/.php_cs.cache
/.php-cs-fixer.cache
/.phpunit.result.cache
/composer.lock
/vendor/
39 changes: 39 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of JoliTypo - a project by JoliCode.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

$fileHeaderComment = <<<'EOF'
This file is part of JoliTypo - a project by JoliCode.
This software consists of voluntary contributions made by many individuals
and is licensed under the MIT license.
EOF;

$finder = PhpCsFixer\Finder::create()
->in(__DIR__)
->notPath('src/JoliTypo/Bridge/Symfony/DependencyInjection/Configuration.php')
->append([
__FILE__,
])
;

return (new PhpCsFixer\Config())
->setRiskyAllowed(true)
->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'@PHP74Migration' => true,
'@PhpCsFixer' => true,
'php_unit_internal_class' => false, // From @PhpCsFixer but we don't want it
'php_unit_test_class_requires_covers' => false, // From @PhpCsFixer but we don't want it
'phpdoc_add_missing_param_annotation' => false, // From @PhpCsFixer but we don't want it
'header_comment' => ['header' => $fileHeaderComment],
'concat_space' => ['spacing' => 'one'],
])
->setFinder($finder)
;
22 changes: 0 additions & 22 deletions .php_cs

This file was deleted.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"org_heigl/hyphenator": "~2.6.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2",
"friendsofphp/php-cs-fixer": "^3.3.2",
"symfony/phpunit-bridge": "^5.0",
"symfony/framework-bundle": "^3.4.26|^4.1.12|^5.0",
"symfony/twig-bundle": "^3.4.26|^4.1.12|^5.0",
Expand Down
201 changes: 99 additions & 102 deletions src/JoliTypo/Fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,31 @@ class Fixer
* DOMDocument does not like all the HTML entities; sometimes they are double encoded.
* So the entities here are plain utf8 and DOCDocument::saveHTML transform them to entity.
*/
const NO_BREAK_THIN_SPACE = "\xE2\x80\xAF"; // &#8239;
const NO_BREAK_SPACE = "\xC2\xA0"; // &#160;
const ELLIPSIS = '';
const LAQUO = '«'; // &laquo;
const RAQUO = '»'; // &raquo;
const RSQUO = ''; // &rsquo;
const TIMES = '×'; // &times;
const NDASH = ''; // &ndash; or &#x2013;
const MDASH = ''; // &mdash; or &#x2014;
const LDQUO = ''; // &ldquo; or &#8220;
const RDQUO = ''; // &rdquo; or &#8221;
const BDQUO = ''; // &bdquo; or &#8222;
const SHY = "\xC2\xAD"; // &shy;
const TRADE = ''; // &trade;
const REG = '®'; // &reg;
const COPY = '©'; // &copy;
const ALL_SPACES = "\xE2\x80\xAF|\xC2\xAD|\xC2\xA0|\\s"; // All supported spaces, used in regexps. Better than \s

const RECOMMENDED_RULES_BY_LOCALE = [
public const NO_BREAK_THIN_SPACE = "\xE2\x80\xAF"; // &#8239;
public const NO_BREAK_SPACE = "\xC2\xA0"; // &#160;
public const ELLIPSIS = '';
public const LAQUO = '«'; // &laquo;
public const RAQUO = '»'; // &raquo;
public const RSQUO = ''; // &rsquo;
public const TIMES = '×'; // &times;
public const NDASH = ''; // &ndash; or &#x2013;
public const MDASH = ''; // &mdash; or &#x2014;
public const LDQUO = ''; // &ldquo; or &#8220;
public const RDQUO = ''; // &rdquo; or &#8221;
public const BDQUO = ''; // &bdquo; or &#8222;
public const SHY = "\xC2\xAD"; // &shy;
public const TRADE = ''; // &trade;
public const REG = '®'; // &reg;
public const COPY = '©'; // &copy;
public const ALL_SPACES = "\xE2\x80\xAF|\xC2\xAD|\xC2\xA0|\\s"; // All supported spaces, used in regexps. Better than \s

public const RECOMMENDED_RULES_BY_LOCALE = [
'en_GB' => ['Ellipsis', 'Dimension', 'Unit', 'Dash', 'SmartQuotes', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'],
'fr_FR' => ['Ellipsis', 'Dimension', 'Unit', 'Dash', 'SmartQuotes', 'FrenchNoBreakSpace', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'],
'fr_CA' => ['Ellipsis', 'Dimension', 'Unit', 'Dash', 'SmartQuotes', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'],
'de_DE' => ['Ellipsis', 'Dimension', 'Unit', 'Dash', 'SmartQuotes', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'],
];


/**
* @var array HTML Tags to bypass
*/
Expand Down Expand Up @@ -93,9 +92,7 @@ public function fix($content)

$this->processDOM($dom, $dom);

$content = $this->exportDOMDocument($dom);

return $content;
return $this->exportDOMDocument($dom);
}

/**
Expand Down Expand Up @@ -124,6 +121,73 @@ public function setRules($rules)
$this->compileRules($rules);
}

/**
* Customize the list of protected tags.
*
* @param array $protectedTags
*
* @throws \InvalidArgumentException
*/
public function setProtectedTags($protectedTags)
{
if (!\is_array($protectedTags)) {
throw new \InvalidArgumentException('Protected tags must be an array (empty array for no protection).');
}

$this->protectedTags = $protectedTags;
}

/**
* Get the current Locale tag.
*
* @return string
*/
public function getLocale()
{
return $this->locale;
}

/**
* Change the locale of the Fixer.
*
* @param string $locale An IETF language tag
*
* @throws \InvalidArgumentException
*/
public function setLocale($locale)
{
if (!\is_string($locale) || empty($locale)) {
throw new \InvalidArgumentException('Locale must be an IETF language tag.');
}

// Set the Locale on Fixer that needs it
foreach ($this->_rules as $rule) {
if ($rule instanceof LocaleAwareFixerInterface) {
$rule->setLocale($locale);
}
}

$this->locale = $locale;
}

/**
* Get language part of a Locale string (fr_FR => fr).
*
* @param $locale
*
* @return string
*/
public static function getLanguageFromLocale($locale)
{
if (strpos($locale, '_')) {
$parts = explode('_', $locale);

return strtolower($parts[0]);
}

return $locale;
}

/**
* Build the _rules array of Fixer.
*
Expand All @@ -133,19 +197,19 @@ public function setRules($rules)
*/
private function compileRules($rules)
{
if (!is_array($rules) || empty($rules)) {
if (!\is_array($rules) || empty($rules)) {
throw new BadRuleSetException('Rules must be an array of Fixer');
}

$this->_rules = [];
foreach ($rules as $rule) {
if (is_object($rule)) {
if (\is_object($rule)) {
$fixer = $rule;
$className = get_class($rule);
$className = \get_class($rule);
} else {
$className = class_exists($rule) ? $rule : (class_exists(
'JoliTypo\\Fixer\\'.$rule
) ? 'JoliTypo\\Fixer\\'.$rule : false);
'JoliTypo\\Fixer\\' . $rule
) ? 'JoliTypo\\Fixer\\' . $rule : false);
if (!$className) {
throw new BadRuleSetException(sprintf('Fixer %s not found', $rule));
}
Expand Down Expand Up @@ -174,7 +238,7 @@ private function processDOM(\DOMNode $node, \DOMDocument $dom)
$nodes = [];
foreach ($node->childNodes as $childNode) {
if ($childNode instanceof \DOMElement && $childNode->tagName) {
if (in_array($childNode->tagName, $this->protectedTags)) {
if (\in_array($childNode->tagName, $this->protectedTags)) {
continue;
}
}
Expand Down Expand Up @@ -228,9 +292,9 @@ private function doFix(\DOMText $childNode, \DOMNode $node, \DOMDocument $dom)
/**
* @param $content
*
* @return \DOMDocument
*
* @throws Exception\InvalidMarkupException
*
* @return \DOMDocument
*/
private function loadDOMDocument($content)
{
Expand Down Expand Up @@ -278,7 +342,7 @@ private function fixContentEncoding($content)
$content,
'<body'
) ? '<?xml encoding="UTF-8"><body>' : '<?xml encoding="UTF-8">';
$content = $hack.$content;
$content = $hack . $content;
}

$encoding = mb_detect_encoding($content);
Expand All @@ -287,8 +351,8 @@ private function fixContentEncoding($content)
// Add a meta to the <head> section
if (false !== $headPos) {
$headPos += 6;
$content = mb_substr($content, 0, $headPos).
'<meta http-equiv="Content-Type" content="text/html; charset='.$encoding.'">'.
$content = mb_substr($content, 0, $headPos) .
'<meta http-equiv="Content-Type" content="text/html; charset=' . $encoding . '">' .
mb_substr($content, $headPos);
}

Expand All @@ -306,7 +370,7 @@ private function exportDOMDocument(\DOMDocument $dom)
// Remove added body & doctype
$content = preg_replace(
[
"/^\<\!DOCTYPE.*?<html>.*?<body>/si",
'/^\\<\\!DOCTYPE.*?<html>.*?<body>/si',
'!</body>\n?</html>$!si',
],
'',
Expand All @@ -315,71 +379,4 @@ private function exportDOMDocument(\DOMDocument $dom)

return trim($content);
}

/**
* Customize the list of protected tags.
*
* @param array $protectedTags
*
* @throws \InvalidArgumentException
*/
public function setProtectedTags($protectedTags)
{
if (!is_array($protectedTags)) {
throw new \InvalidArgumentException('Protected tags must be an array (empty array for no protection).');
}

$this->protectedTags = $protectedTags;
}

/**
* Get the current Locale tag.
*
* @return string
*/
public function getLocale()
{
return $this->locale;
}

/**
* Change the locale of the Fixer.
*
* @param string $locale An IETF language tag
*
* @throws \InvalidArgumentException
*/
public function setLocale($locale)
{
if (!is_string($locale) || empty($locale)) {
throw new \InvalidArgumentException('Locale must be an IETF language tag.');
}

// Set the Locale on Fixer that needs it
foreach ($this->_rules as $rule) {
if ($rule instanceof LocaleAwareFixerInterface) {
$rule->setLocale($locale);
}
}

$this->locale = $locale;
}

/**
* Get language part of a Locale string (fr_FR => fr).
*
* @param $locale
*
* @return string
*/
public static function getLanguageFromLocale($locale)
{
if (strpos($locale, '_')) {
$parts = explode('_', $locale);

return strtolower($parts[0]);
}

return $locale;
}
}
4 changes: 2 additions & 2 deletions src/JoliTypo/Fixer/BaseOpenClosePair.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ protected function fixViaState($content, StateBag $stateBag, $stateName, $openRe
// If we have a open sibling and we detect a closing quote
} elseif ($storedSibling instanceof StateNode && preg_match($closeRegexp, $content)) {
// Replace the closing tag
$content = preg_replace($closeRegexp, '$1'.$closeReplacement.'$2', $content, 1);
$content = preg_replace($closeRegexp, '$1' . $closeReplacement . '$2', $content, 1);

// Replace the opening tag
$open_content = preg_replace($openRegexp, '$1'.$openReplacement.'$2', $storedSibling->getNode()->wholeText, 1);
$open_content = preg_replace($openRegexp, '$1' . $openReplacement . '$2', $storedSibling->getNode()->wholeText, 1);

$stateBag->fixSiblingNode($stateName, $open_content);
}
Expand Down
2 changes: 1 addition & 1 deletion src/JoliTypo/Fixer/CurlyQuote.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ class CurlyQuote implements FixerInterface
{
public function fix($content, StateBag $stateBag = null)
{
return preg_replace('@([a-z])\'@im', '$1'.Fixer::RSQUO, $content);
return preg_replace('@([a-z])\'@im', '$1' . Fixer::RSQUO, $content);
}
}
3 changes: 1 addition & 2 deletions src/JoliTypo/Fixer/Dash.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ class Dash implements FixerInterface
public function fix($content, StateBag $stateBag = null)
{
$content = preg_replace('@(?<=[0-9 ]|^)-(?=[0-9 ]|$)@', Fixer::NDASH, $content);
$content = preg_replace('@ ?-- ?([^-]|$)@s', Fixer::MDASH.'$1', $content);

return $content;
return preg_replace('@ ?-- ?([^-]|$)@s', Fixer::MDASH . '$1', $content);
}
}
Loading

0 comments on commit 4f6d740

Please sign in to comment.