Skip to content

Commit

Permalink
Fix PHPStan issue
Browse files Browse the repository at this point in the history
  • Loading branch information
schlessera committed May 25, 2021
1 parent 2af9cfb commit a2f6e51
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Dom/LinkManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace AmpProject\Dom;

use AmpProject\Attribute;
use AmpProject\Exception\FailedToCreateLink;
use AmpProject\RequestDestination;
use AmpProject\Tag;
use DOMNode;
Expand Down Expand Up @@ -258,13 +259,16 @@ public function add($rel, $href, $attributes = [])
$this->referenceNode = $this->document->viewport;
}

/** @var Element $link */
if ($this->referenceNode) {
$link = $this->document->head->insertBefore($link, $this->referenceNode->nextSibling);
} else {
$link = $this->document->head->appendChild($link);
}

if (! $link instanceof Element) {
throw FailedToCreateLink::forLink($link);
}

$this->links[$this->getKey($link)] = $link;

$this->referenceNode = $link;
Expand Down
29 changes: 29 additions & 0 deletions src/Exception/FailedToCreateLink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace AmpProject\Exception;

use RuntimeException;

/**
* Exception thrown when a link could not be created.
*
* @package ampproject/amp-toolbox
*/
final class FailedToCreateLink extends RuntimeException implements AmpException
{

/**
* Instantiate a FailedToCreateLink exception for a link that could not be created.
*
* @param mixed $link Link that was not as expected.
* @return self
*/
public static function forLink($link)
{
$type = is_object($link) ? get_class($link) : gettype($link);
$message = "Failed to create a link via the link manager. "
. "Expected to produce an 'AmpProject\\Dom\\Element', got '$type' instead.";

return new self($message);
}
}

0 comments on commit a2f6e51

Please sign in to comment.