Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate PHPStan #215

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
* text=auto

/example export-ignore
/tests export-ignore
/.editorconfig export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/phpunit.xml.dist export-ignore
/CHANGELOG.md export-ignore
/example export-ignore
/phpstan.neon.dist export-ignore
/phpunit.xml.dist export-ignore
/README.md export-ignore
/stubs export-ignore
/tests export-ignore
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ install:
- if [[ $setup = 'basic' && "$TRAVIS_PHP_VERSION" != "nightly" ]]; then travis_retry composer install --prefer-dist --no-interaction; fi
- if [[ $setup = 'lowest' && "$TRAVIS_PHP_VERSION" != "nightly" ]]; then travis_retry composer update --prefer-dist --no-interaction --prefer-lowest --prefer-stable; fi
- if [[ "$TRAVIS_PHP_VERSION" = "nightly" ]]; then travis_retry composer install --prefer-dist --no-interaction --ignore-platform-reqs; fi
- if [[ "$TRAVIS_PHP_VERSION" = "7.4" ]]; then travis_retry composer requrie --dev phpstan/phpstan:1.1.2; fi

script:
- if [[ "$TRAVIS_PHP_VERSION" = "7.4" ]]; then composer require vendor/bin/phpstan; fi
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

composer require looks wrong here

Copy link
Author

@bytestream bytestream Nov 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yeah..

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be none of it, as this is expected to run phpstan, not to install it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated it 9f076e6

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also worth pointing out that travis isn't running anymore... Perhaps should switch to GitHub Actions in another PR?

- vendor/bin/phpunit --verbose --coverage-clover=coverage.clover

after_success:
Expand Down
6 changes: 6 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
level: max
paths:
- src/
scanFiles:
- stubs/CssSelector.stub
11 changes: 6 additions & 5 deletions src/Css/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function getCssFromStyleTags($html)
{
$css = '';
$matches = array();
$htmlNoComments = preg_replace('|<!--.*?-->|s', '', $html);
$htmlNoComments = (string) preg_replace('|<!--.*?-->|s', '', $html);
preg_match_all('|<style(?:\s.*)?>(.*)</style>|isU', $htmlNoComments, $matches);

if (!empty($matches[1])) {
Expand All @@ -55,15 +55,16 @@ public function getCssFromStyleTags($html)
private function doCleanup($css)
{
// remove charset
$css = preg_replace('/@charset "[^"]++";/', '', $css);
$css = (string) preg_replace('/@charset "[^"]++";/', '', $css);
// remove media queries
$css = preg_replace('/@media [^{]*+{([^{}]++|{[^{}]*+})*+}/', '', $css);
$css = (string) preg_replace('/@media [^{]*+{([^{}]++|{[^{}]*+})*+}/', '', $css);

$css = str_replace(array("\r", "\n"), '', $css);
$css = str_replace(array("\t"), ' ', $css);
$css = str_replace('"', '\'', $css);
$css = preg_replace('|/\*.*?\*/|', '', $css);
$css = preg_replace('/\s\s++/', ' ', $css);
$css = (string) preg_replace('|/\*.*?\*/|', '', $css);
$css = (string) preg_replace('/\s\s++/', ' ', $css);

$css = trim($css);

return $css;
Expand Down
4 changes: 2 additions & 2 deletions src/Css/Property/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ private function cleanup($string)
$string = str_replace(array("\r", "\n"), '', $string);
$string = str_replace(array("\t"), ' ', $string);
$string = str_replace('"', '\'', $string);
$string = preg_replace('|/\*.*?\*/|', '', $string);
$string = preg_replace('/\s\s+/', ' ', $string);
$string = (string) preg_replace('|/\*.*?\*/|', '', $string);
$string = (string) preg_replace('/\s\s+/', ' ', $string);

$string = trim($string);
$string = rtrim($string, ';');
Expand Down
4 changes: 2 additions & 2 deletions src/Css/Property/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class Property
private $value;

/**
* @var Specificity
* @var Specificity|null
*/
private $originalSpecificity;

Expand Down Expand Up @@ -57,7 +57,7 @@ public function getValue()
/**
* Get originalSpecificity
*
* @return Specificity
* @return Specificity|null
*/
public function getOriginalSpecificity()
{
Expand Down
10 changes: 5 additions & 5 deletions src/Css/Rule/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ private function cleanup($string)
$string = str_replace(array("\r", "\n"), '', $string);
$string = str_replace(array("\t"), ' ', $string);
$string = str_replace('"', '\'', $string);
$string = preg_replace('|/\*.*?\*/|', '', $string);
$string = preg_replace('/\s\s+/', ' ', $string);
$string = (string) preg_replace('|/\*.*?\*/|', '', $string);
$string = (string) preg_replace('/\s\s+/', ' ', $string);

$string = trim($string);
$string = rtrim($string, '}');
Expand Down Expand Up @@ -116,9 +116,9 @@ public function calculateSpecificityBasedOnASelector($selector)
)";

return new Specificity(
preg_match_all("/{$idSelectorsPattern}/ix", $selector, $matches),
preg_match_all("/{$classAttributesPseudoClassesSelectorsPattern}/ix", $selector, $matches),
preg_match_all("/{$typePseudoElementsSelectorPattern}/ix", $selector, $matches)
preg_match_all("/{$idSelectorsPattern}/ix", $selector, $matches) ?: 0,
preg_match_all("/{$classAttributesPseudoClassesSelectorsPattern}/ix", $selector, $matches) ?: 0,
preg_match_all("/{$typePseudoElementsSelectorPattern}/ix", $selector, $matches) ?: 0
);
}

Expand Down
15 changes: 11 additions & 4 deletions src/CssToInlineStyles.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

class CssToInlineStyles
{
/** @var CssSelectorConverter */
private $cssConverter;

public function __construct()
Expand Down Expand Up @@ -130,12 +131,15 @@ protected function getHtmlFromDocument(\DOMDocument $document)
// retrieve the document element
// we do it this way to preserve the utf-8 encoding
$htmlElement = $document->documentElement;
$html = $document->saveHTML($htmlElement);
$html = $document->saveHTML($htmlElement) ?: '';
$html = trim($html);
if ($htmlElement === null) {
return $html;
}

// retrieve the doctype
$document->removeChild($htmlElement);
$doctype = $document->saveHTML();
$doctype = $document->saveHTML() ?: '';
$doctype = trim($doctype);

// if it is the html5 doctype convert it to lowercase
Expand All @@ -158,6 +162,7 @@ protected function inline(\DOMDocument $document, array $rules)
return $document;
}

/** @var \SplObjectStorage<\DOMElement, Css\Property\Property[]> $propertyStorage */
$propertyStorage = new \SplObjectStorage();

$xPath = new \DOMXPath($document);
Expand Down Expand Up @@ -222,8 +227,10 @@ private function calculatePropertiesToBeApplied(array $properties, array $cssPro

//overrule if current property is important and existing is not, else check specificity
$overrule = !$existingProperty->isImportant() && $property->isImportant();
if (!$overrule) {
$overrule = $existingProperty->getOriginalSpecificity()->compareTo($property->getOriginalSpecificity()) <= 0;
$originalSpecificity = $existingProperty->getOriginalSpecificity();
$specificity = $property->getOriginalSpecificity();
if (!$overrule && $originalSpecificity !== null && $specificity !== null) {
$overrule = $originalSpecificity->compareTo($specificity) <= 0;
}

if ($overrule) {
Expand Down
34 changes: 34 additions & 0 deletions stubs/CssSelector.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Symfony\Component\CssSelector;

class CssSelector
{
/** @var bool */
private static $html = true;

/**
* @param mixed $cssExpr The CSS expression
* @param string $prefix An optional prefix for the XPath expression
*
* @return string
*/
public static function toXPath($cssExpr, $prefix = 'descendant-or-self::')
{
$converter = new CssSelectorConverter(self::$html);

return $converter->toXPath($cssExpr, $prefix);
}

/** @return void */
public static function enableHtmlExtension()
{
self::$html = true;
}

/** @return void */
public static function disableHtmlExtension()
{
self::$html = false;
}
}