Skip to content

Commit

Permalink
[CLEANUP] Make use of PHP 7.0 language features
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverklee committed Oct 1, 2019
1 parent 82b7125 commit 40a8d58
Show file tree
Hide file tree
Showing 19 changed files with 184 additions and 166 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
### Added

### Changed
- Make use of PHP 7.0 language features
[#777](https://github.com/MyIntervals/emogrifier/pull/777))

### Deprecated
- Support for PHP 7.0 will be removed in Emogrifier 5.0.
Expand Down
13 changes: 8 additions & 5 deletions config/php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

if (PHP_SAPI !== 'cli') {
die('This script supports command line usage only. Please check your command.');
}
Expand All @@ -11,17 +13,19 @@
'@DoctrineAnnotation' => true,
'@PHP56Migration' => true,
'@PHP56Migration:risky' => true,
'@PHP70Migration' => true,
'@PHP70Migration:risky' => true,
'@PHPUnit57Migration:risky' => true,
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'blank_line_after_opening_tag' => true,
'cast_spaces' => ['space' => 'none'],
'combine_consecutive_issets' => true,
'combine_consecutive_unsets' => true,
'compact_nullable_typehint' => true,
'concat_space' => ['spacing' => 'one'],
'declare_equal_normalize' => ['space' => 'single'],
// PHP >= 7.0
// 'declare_strict_types' => true,
'declare_equal_normalize' => true,
'declare_strict_types' => true,
'dir_constant' => true,
'elseif' => true,
'encoding' => true,
Expand Down Expand Up @@ -85,8 +89,7 @@
'space_after_semicolon' => true,
'standardize_not_equals' => true,
'ternary_operator_spaces' => true,
// PHP >= 7.0
// 'ternary_to_null_coalescing' => true,
'ternary_to_null_coalescing' => true,
'trailing_comma_in_multiline_array' => true,
'unary_operator_spaces' => true,
'whitespace_after_comma_in_array' => true,
Expand Down
62 changes: 32 additions & 30 deletions src/Emogrifier/CssInliner.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Pelago\Emogrifier;

use Pelago\Emogrifier\HtmlProcessor\AbstractHtmlProcessor;
Expand Down Expand Up @@ -154,7 +156,7 @@ class CssInliner extends AbstractHtmlProcessor
*
* @throws SyntaxErrorException
*/
public function inlineCss($css = '')
public function inlineCss(string $css = ''): self
{
$this->clearAllCaches();
$this->purgeVisitedNodes();
Expand Down Expand Up @@ -233,7 +235,7 @@ public function disableStyleBlocksParsing()
*
* @return void
*/
public function addAllowedMediaType($mediaName)
public function addAllowedMediaType(string $mediaName)
{
$this->allowedMediaTypes[$mediaName] = true;
}
Expand All @@ -245,7 +247,7 @@ public function addAllowedMediaType($mediaName)
*
* @return void
*/
public function removeAllowedMediaType($mediaName)
public function removeAllowedMediaType(string $mediaName)
{
if (isset($this->allowedMediaTypes[$mediaName])) {
unset($this->allowedMediaTypes[$mediaName]);
Expand All @@ -261,7 +263,7 @@ public function removeAllowedMediaType($mediaName)
*
* @return void
*/
public function addExcludedSelector($selector)
public function addExcludedSelector(string $selector)
{
$this->excludedSelectors[$selector] = true;
}
Expand All @@ -273,7 +275,7 @@ public function addExcludedSelector($selector)
*
* @return void
*/
public function removeExcludedSelector($selector)
public function removeExcludedSelector(string $selector)
{
if (isset($this->excludedSelectors[$selector])) {
unset($this->excludedSelectors[$selector]);
Expand All @@ -287,7 +289,7 @@ public function removeExcludedSelector($selector)
*
* @return void
*/
public function setDebug($debug)
public function setDebug(bool $debug)
{
$this->debug = $debug;
}
Expand All @@ -302,7 +304,7 @@ public function setDebug($debug)
*
* @throws \BadMethodCallException if `inlineCss` has not been called first
*/
public function getMatchingUninlinableSelectors()
public function getMatchingUninlinableSelectors(): array
{
if ($this->matchingUninlinableCssRules === null) {
throw new \BadMethodCallException('inlineCss must be called first', 1568385221);
Expand Down Expand Up @@ -365,7 +367,7 @@ private function normalizeStyleAttributesOfAllNodes()
*
* @return \DOMNodeList
*/
private function getAllNodesWithStyleAttribute()
private function getAllNodesWithStyleAttribute(): \DOMNodeList
{
return $this->xPath->query('//*[@style]');
}
Expand Down Expand Up @@ -417,7 +419,7 @@ static function (array $m) {
* @return string[]
* the CSS declarations with the property names as array keys and the property values as array values
*/
private function parseCssDeclarationsBlock($cssDeclarationsBlock)
private function parseCssDeclarationsBlock(string $cssDeclarationsBlock): array
{
if (isset($this->caches[self::CACHE_KEY_CSS_DECLARATIONS_BLOCK][$cssDeclarationsBlock])) {
return $this->caches[self::CACHE_KEY_CSS_DECLARATIONS_BLOCK][$cssDeclarationsBlock];
Expand All @@ -444,7 +446,7 @@ private function parseCssDeclarationsBlock($cssDeclarationsBlock)
*
* @return string
*/
private function getCssFromAllStyleNodes()
private function getCssFromAllStyleNodes(): string
{
$styleNodes = $this->xPath->query('//style');
if ($styleNodes === false) {
Expand All @@ -468,7 +470,7 @@ private function getCssFromAllStyleNodes()
*
* @return string CSS with the comments removed
*/
private function removeCssComments($css)
private function removeCssComments(string $css): string
{
return \preg_replace('%/\\*[^*]*+(?:\\*(?!/)[^*]*+)*+\\*/%', '', $css);
}
Expand All @@ -486,7 +488,7 @@ private function removeCssComments($css)
* in the original CSS (so that either unminified or minified formatting is preserved); if there were no `@import`
* rules, it will be an empty string. The (valid) `@charset` rules are discarded.
*/
private function extractImportAndCharsetRules($css)
private function extractImportAndCharsetRules(string $css): array
{
$possiblyModifiedCss = $css;
$importRules = '';
Expand Down Expand Up @@ -517,7 +519,7 @@ private function extractImportAndCharsetRules($css)
*
* @throws SyntaxErrorException
*/
private function getNodesToExclude()
private function getNodesToExclude(): array
{
$excludedNodes = [];
foreach (\array_keys($this->excludedSelectors) as $selectorToExclude) {
Expand All @@ -540,7 +542,7 @@ private function getNodesToExclude()
/**
* @return CssSelectorConverter
*/
private function getCssSelectorConverter()
private function getCssSelectorConverter(): CssSelectorConverter
{
if ($this->cssSelectorConverter === null) {
$this->cssSelectorConverter = new CssSelectorConverter();
Expand All @@ -566,7 +568,7 @@ private function getCssSelectorConverter()
* e.g., "color: red; height: 4px;"),
* and "line" (the line number e.g. 42)
*/
private function parseCssRules($css)
private function parseCssRules(string $css): array
{
$cssKey = \md5($css);
if (isset($this->caches[self::CACHE_KEY_CSS][$cssKey])) {
Expand Down Expand Up @@ -623,7 +625,7 @@ private function parseCssRules($css)
*
* @return int
*/
private function sortBySelectorPrecedence(array $a, array $b)
private function sortBySelectorPrecedence(array $a, array $b): int
{
$precedenceA = $this->getCssSelectorPrecedence($a['selector']);
$precedenceB = $this->getCssSelectorPrecedence($b['selector']);
Expand All @@ -640,7 +642,7 @@ private function sortBySelectorPrecedence(array $a, array $b)
*
* @return int
*/
private function getCssSelectorPrecedence($selector)
private function getCssSelectorPrecedence(string $selector): int
{
$selectorKey = \md5($selector);
if (isset($this->caches[self::CACHE_KEY_SELECTOR][$selectorKey])) {
Expand Down Expand Up @@ -673,7 +675,7 @@ private function getCssSelectorPrecedence($selector)
* "declarations" (the semicolon-separated CSS declarations for that/those selector(s),
* e.g., "color: red; height: 4px;"),
*/
private function getCssRuleMatches($css)
private function getCssRuleMatches(string $css): array
{
$splitCss = $this->splitCssAndMediaQuery($css);

Expand Down Expand Up @@ -724,7 +726,7 @@ private function getCssRuleMatches($css)
*
* @return string[][]
*/
private function splitCssAndMediaQuery($css)
private function splitCssAndMediaQuery(string $css): array
{
$mediaTypesExpression = '';
if (!empty($this->allowedMediaTypes)) {
Expand Down Expand Up @@ -781,7 +783,7 @@ private function splitCssAndMediaQuery($css)
*/
private function copyInlinableCssToStyleAttribute(\DOMElement $node, array $cssRule)
{
$newStyleDeclarations = $this->parseCssDeclarationsBlock($cssRule['declarationsBlock']);
$newStyleDeclarations = $this->parseCssDeclarationsBlock((string)$cssRule['declarationsBlock']);
if ($newStyleDeclarations === []) {
return;
}
Expand Down Expand Up @@ -810,7 +812,7 @@ private function copyInlinableCssToStyleAttribute(\DOMElement $node, array $cssR
*
* @return string
*/
private function generateStyleStringFromDeclarationsArrays(array $oldStyles, array $newStyles)
private function generateStyleStringFromDeclarationsArrays(array $oldStyles, array $newStyles): string
{
$cacheKey = \serialize([$oldStyles, $newStyles]);
if (isset($this->caches[self::CACHE_KEY_COMBINED_STYLES][$cacheKey])) {
Expand Down Expand Up @@ -854,7 +856,7 @@ private function generateStyleStringFromDeclarationsArrays(array $oldStyles, arr
*
* @return bool
*/
private function attributeValueIsImportant($attributeValue)
private function attributeValueIsImportant(string $attributeValue): bool
{
return \strtolower(\substr(\trim($attributeValue), -10)) === '!important';
}
Expand Down Expand Up @@ -933,7 +935,7 @@ private function removeImportantAnnotationFromNodeInlineStyle(\DOMElement $node)
*
* @return string
*/
private function generateStyleStringFromSingleDeclarationsArray(array $styleDeclarations)
private function generateStyleStringFromSingleDeclarationsArray(array $styleDeclarations): string
{
return $this->generateStyleStringFromDeclarationsArrays([], $styleDeclarations);
}
Expand Down Expand Up @@ -964,7 +966,7 @@ private function determineMatchingUninlinableCssRules(array $cssRules)
*
* @throws SyntaxErrorException
*/
private function existsMatchForSelectorInCssRule(array $cssRule)
private function existsMatchForSelectorInCssRule(array $cssRule): bool
{
$selector = $cssRule['selector'];
if ($cssRule['hasUnmatchablePseudo']) {
Expand All @@ -984,7 +986,7 @@ private function existsMatchForSelectorInCssRule(array $cssRule)
*
* @throws SyntaxErrorException
*/
private function existsMatchForCssSelector($cssSelector)
private function existsMatchForCssSelector(string $cssSelector): bool
{
try {
$nodesMatchingSelector = $this->xPath->query($this->getCssSelectorConverter()->toXPath($cssSelector));
Expand All @@ -1007,7 +1009,7 @@ private function existsMatchForCssSelector($cssSelector)
* @return string Selector which will match the relevant DOM elements if the pseudo-classes are assumed to apply,
* or in the case of pseudo-elements will match their originating element.
*/
private function removeUnmatchablePseudoComponents($selector)
private function removeUnmatchablePseudoComponents(string $selector): string
{
// The regex allows nested brackets via `(?2)`.
// A space is temporarily prepended because the callback can't determine if the match was at the very start.
Expand All @@ -1034,7 +1036,7 @@ private function removeUnmatchablePseudoComponents($selector)
* @return string the full match if there were no unmatchable pseudo components within; otherwise, any preceding
* whitespace followed by "*", or an empty string if there was no preceding whitespace
*/
private function replaceUnmatchableNotComponent(array $matches)
private function replaceUnmatchableNotComponent(array $matches): string
{
list($notComponentWithAnyPrecedingWhitespace, $anyPrecedingWhitespace, $notArgumentInBrackets) = $matches;

Expand All @@ -1060,7 +1062,7 @@ private function replaceUnmatchableNotComponent(array $matches)
*
* @return void
*/
private function copyUninlinableCssToStyleNode($cssImportRules)
private function copyUninlinableCssToStyleNode(string $cssImportRules)
{
$css = $cssImportRules;

Expand Down Expand Up @@ -1090,7 +1092,7 @@ private function copyUninlinableCssToStyleNode($cssImportRules)
*
* @return void
*/
protected function addStyleElementToDocument($css)
protected function addStyleElementToDocument(string $css)
{
$styleElement = $this->domDocument->createElement('style', $css);
$styleAttribute = $this->domDocument->createAttribute('type');
Expand All @@ -1108,7 +1110,7 @@ protected function addStyleElementToDocument($css)
*
* @return \DOMElement
*/
private function getHeadElement()
private function getHeadElement(): \DOMElement
{
return $this->domDocument->getElementsByTagName('head')->item(0);
}
Expand Down
Loading

0 comments on commit 40a8d58

Please sign in to comment.