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

[BUGFIX] partially satisfy psalm for src/HtmlProcessor/AbstractHtmlProcessor.php #805

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: 6 additions & 3 deletions psalm.baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@
<code>null</code>
<code>null</code>
</PossiblyNullPropertyAssignmentValue>
<PossiblyNullReference occurrences="2">
<code>createElement</code>
<code>getElementsByTagName</code>
</PossiblyNullReference>
<UnusedMethod occurrences="1">
<code>replaceUnmatchableNotComponent</code>
</UnusedMethod>
Expand All @@ -93,10 +97,9 @@
<code>static::PHP_UNRECOGNIZED_VOID_TAGNAME_MATCHER</code>
</MixedOperand>
<NullableReturnStatement occurrences="1">
<code>$this-&gt;domDocument-&gt;getElementsByTagName('body')-&gt;item(0)</code>
<code>$this-&gt;getDomDocument()-&gt;getElementsByTagName('body')-&gt;item(0)</code>
</NullableReturnStatement>
<PossiblyNullPropertyAssignmentValue occurrences="2">
<code>null</code>
<PossiblyNullPropertyAssignmentValue occurrences="1">
<code>null</code>
</PossiblyNullPropertyAssignmentValue>
</file>
Expand Down
27 changes: 20 additions & 7 deletions src/HtmlProcessor/AbstractHtmlProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ abstract class AbstractHtmlProcessor
const PHP_UNRECOGNIZED_VOID_TAGNAME_MATCHER = '(?:command|embed|keygen|source|track|wbr)';

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

Expand Down Expand Up @@ -103,9 +103,22 @@ private function setHtml(string $html)
* Provides access to the internal DOMDocument representation of the HTML in its current state.
*
* @return \DOMDocument
*
* @throws \UnexpectedValueException
*/
public function getDomDocument(): \DOMDocument
{
if ($this->domDocument === null) {
throw new \UnexpectedValueException(
(
self::class .
'::setDomDocument() has not yet been called on ' .
static::class
),
1570472239
);
}

return $this->domDocument;
}

Expand All @@ -127,7 +140,7 @@ private function setDomDocument(\DOMDocument $domDocument)
*/
public function render(): string
{
$htmlWithPossibleErroneousClosingTags = $this->domDocument->saveHTML();
$htmlWithPossibleErroneousClosingTags = $this->getDomDocument()->saveHTML();

return $this->removeSelfClosingTagsClosingTags($htmlWithPossibleErroneousClosingTags);
}
Expand All @@ -139,7 +152,7 @@ public function render(): string
*/
public function renderBodyContent(): string
{
$htmlWithPossibleErroneousClosingTags = $this->domDocument->saveHTML($this->getBodyElement());
$htmlWithPossibleErroneousClosingTags = $this->getDomDocument()->saveHTML($this->getBodyElement());
$bodyNodeHtml = $this->removeSelfClosingTagsClosingTags($htmlWithPossibleErroneousClosingTags);

return \preg_replace('%</?+body(?:\\s[^>]*+)?+>%', '', $bodyNodeHtml);
Expand All @@ -166,7 +179,7 @@ private function removeSelfClosingTagsClosingTags(string $html): string
*/
private function getBodyElement(): \DOMElement
{
return $this->domDocument->getElementsByTagName('body')->item(0);
return $this->getDomDocument()->getElementsByTagName('body')->item(0);
}

/**
Expand Down Expand Up @@ -299,14 +312,14 @@ private function ensurePhpUnrecognizedSelfClosingTagsAreXml(string $html): strin
*/
private function ensureExistenceOfBodyElement()
{
if ($this->domDocument->getElementsByTagName('body')->item(0) !== null) {
if ($this->getDomDocument()->getElementsByTagName('body')->item(0) !== null) {
return;
}

$htmlElement = $this->domDocument->getElementsByTagName('html')->item(0);
$htmlElement = $this->getDomDocument()->getElementsByTagName('html')->item(0);
if ($htmlElement === null) {
throw new \UnexpectedValueException('There is no HTML element although there should be one.', 1569930853);
}
$htmlElement->appendChild($this->domDocument->createElement('body'));
$htmlElement->appendChild($this->getDomDocument()->createElement('body'));
}
}