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

[CLEANUP] Fix Psalm warnings in AbstractHtmlProcessor #991

Merged
merged 3 commits into from
Apr 11, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 0 additions & 9 deletions psalm.baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,6 @@
<code>string[]</code>
</UnnecessaryVarAnnotation>
</file>
<file src="src/HtmlProcessor/AbstractHtmlProcessor.php">
<PossiblyNullPropertyAssignmentValue occurrences="1">
<code>null</code>
</PossiblyNullPropertyAssignmentValue>
<UnsafeInstantiation occurrences="2">
<code>new static()</code>
<code>new static()</code>
</UnsafeInstantiation>
</file>
<file src="src/HtmlProcessor/CssToAttributeConverter.php">
<MixedArgument occurrences="3">
<code>$mapping['attribute']</code>
Expand Down
12 changes: 7 additions & 5 deletions src/CssInliner.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ public function inlineCss(string $css = ''): self
$cssSelectorConverter = $this->getCssSelectorConverter();
foreach ($cssRules['inlinable'] as $cssRule) {
try {
$nodesMatchingCssSelectors = $this->xPath->query($cssSelectorConverter->toXPath($cssRule['selector']));
$nodesMatchingCssSelectors = $this->getXPath()
->query($cssSelectorConverter->toXPath($cssRule['selector']));
} catch (ParseException $e) {
if ($this->debug) {
throw $e;
Expand Down Expand Up @@ -398,7 +399,7 @@ private function normalizeStyleAttributesOfAllNodes(): void
*/
private function getAllNodesWithStyleAttribute(): \DOMNodeList
{
return $this->xPath->query('//*[@style]');
return $this->getXPath()->query('//*[@style]');
}

/**
Expand Down Expand Up @@ -475,7 +476,7 @@ private function parseCssDeclarationsBlock(string $cssDeclarationsBlock): array
*/
private function getCssFromAllStyleNodes(): string
{
$styleNodes = $this->xPath->query('//style');
$styleNodes = $this->getXPath()->query('//style');
if ($styleNodes === false) {
return '';
}
Expand Down Expand Up @@ -608,7 +609,8 @@ private function getNodesToExclude(): array
$excludedNodes = [];
foreach (\array_keys($this->excludedSelectors) as $selectorToExclude) {
try {
$matchingNodes = $this->xPath->query($this->getCssSelectorConverter()->toXPath($selectorToExclude));
$matchingNodes = $this->getXPath()
->query($this->getCssSelectorConverter()->toXPath($selectorToExclude));
} catch (ParseException $e) {
if ($this->debug) {
throw $e;
Expand Down Expand Up @@ -1110,7 +1112,7 @@ private function existsMatchForSelectorInCssRule(array $cssRule): bool
private function existsMatchForCssSelector(string $cssSelector): bool
{
try {
$nodesMatchingSelector = $this->xPath->query($this->getCssSelectorConverter()->toXPath($cssSelector));
$nodesMatchingSelector = $this->getXPath()->query($this->getCssSelectorConverter()->toXPath($cssSelector));
} catch (ParseException $e) {
if ($this->debug) {
throw $e;
Expand Down
48 changes: 33 additions & 15 deletions src/HtmlProcessor/AbstractHtmlProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* Base class for HTML processor that e.g., can remove, add or modify nodes or attributes.
*
* The "vanilla" subclass is the HtmlNormalizer.
*
* @psalm-consistent-constructor
*/
abstract class AbstractHtmlProcessor
{
Expand Down Expand Up @@ -55,14 +57,14 @@ abstract class AbstractHtmlProcessor
= '%<template[\\s>][^<]*+(?:<(?!/template>)[^<]*+)*+(?:</template>|$)%i';

/**
* @var \DOMDocument|null
* @var ?\DOMDocument
*/
protected $domDocument = null;

/**
* @var \DOMXPath
* @var ?\DOMXPath
*/
protected $xPath = null;
private $xPath = null;

/**
* The constructor.
Expand Down Expand Up @@ -128,15 +130,9 @@ private function setHtml(string $html): void
*/
public function getDomDocument(): \DOMDocument
{
if ($this->domDocument === null) {
throw new \UnexpectedValueException(
(
self::class .
'::setDomDocument() has not yet been called on ' .
static::class
),
1570472239
);
if (!$this->domDocument instanceof \DOMDocument) {
$message = self::class . '::setDomDocument() has not yet been called on ' . static::class;
throw new \UnexpectedValueException($message, 1570472239);
}

return $this->domDocument;
Expand All @@ -151,6 +147,21 @@ private function setDomDocument(\DOMDocument $domDocument): void
$this->xPath = new \DOMXPath($this->domDocument);
}

/**
* @return \DOMXPath
*
* @throws \UnexpectedValueException
*/
protected function getXPath(): \DOMXPath
{
if (!$this->xPath instanceof \DOMXPath) {
$message = self::class . '::setDomDocument() has not yet been called on ' . static::class;
throw new \UnexpectedValueException($message, 1617819086);
}

return $this->xPath;
}

/**
* Renders the normalized and processed HTML.
*
Expand Down Expand Up @@ -194,10 +205,17 @@ private function removeSelfClosingTagsClosingTags(string $html): string
* This method assumes that there always is a BODY element.
*
* @return \DOMElement
*
* @throws \RuntimeException
*/
private function getBodyElement(): \DOMElement
{
return $this->getDomDocument()->getElementsByTagName('body')->item(0);
$node = $this->getDomDocument()->getElementsByTagName('body')->item(0);
if (!$node instanceof \DOMElement) {
throw new \RuntimeException('There is no body element.', 1617922607);
}

return $node;
}

/**
Expand Down Expand Up @@ -441,12 +459,12 @@ private function ensurePhpUnrecognizedSelfClosingTagsAreXml(string $html): strin
*/
private function ensureExistenceOfBodyElement(): void
{
if ($this->getDomDocument()->getElementsByTagName('body')->item(0) !== null) {
if ($this->getDomDocument()->getElementsByTagName('body')->item(0) instanceof \DOMElement) {
return;
}

$htmlElement = $this->getDomDocument()->getElementsByTagName('html')->item(0);
if ($htmlElement === null) {
if (!$htmlElement instanceof \DOMElement) {
throw new \UnexpectedValueException('There is no HTML element although there should be one.', 1569930853);
}
$htmlElement->appendChild($this->getDomDocument()->createElement('body'));
Expand Down
2 changes: 1 addition & 1 deletion src/HtmlProcessor/CssToAttributeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function convertCssToVisualAttributes(): self
*/
private function getAllNodesWithStyleAttribute(): \DOMNodeList
{
return $this->xPath->query('//*[@style]');
return $this->getXPath()->query('//*[@style]');
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/HtmlProcessor/HtmlPruner.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class HtmlPruner extends AbstractHtmlProcessor
*/
public function removeElementsWithDisplayNone(): self
{
$elementsWithStyleDisplayNone = $this->xPath->query(self::DISPLAY_NONE_MATCHER);
$elementsWithStyleDisplayNone = $this->getXPath()->query(self::DISPLAY_NONE_MATCHER);
if ($elementsWithStyleDisplayNone->length === 0) {
return $this;
}
Expand Down Expand Up @@ -61,7 +61,7 @@ public function removeElementsWithDisplayNone(): self
*/
public function removeRedundantClasses(array $classesToKeep = []): self
{
$elementsWithClassAttribute = $this->xPath->query('//*[@class]');
$elementsWithClassAttribute = $this->getXPath()->query('//*[@class]');

if ($classesToKeep !== []) {
$this->removeClassesFromElements($elementsWithClassAttribute, $classesToKeep);
Expand Down