Skip to content

Commit

Permalink
[BUGFIX] Silence purposefully ignored PHP warnings (#400)
Browse files Browse the repository at this point in the history
  • Loading branch information
markhalliwell authored and oliverklee committed Sep 18, 2017
1 parent 23b55f8 commit 25cfbd0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).


### Fixed
- Silence purposefully ignored PHP Warnings
([#400](https://github.com/MyIntervals/emogrifier/pull/400))


### Security
Expand Down
19 changes: 14 additions & 5 deletions Classes/Emogrifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,17 @@ protected function process(\DOMDocument $xmlDocument)
$excludedNodes = $this->getNodesToExclude($xPath);
$cssRules = $this->parseCssRules($cssParts['css']);
foreach ($cssRules as $cssRule) {
// query the body for the xpath selector
$nodesMatchingCssSelectors = $xPath->query($this->translateCssToXpath($cssRule['selector']));
// ignore invalid selectors
if ($nodesMatchingCssSelectors === false) {
// There's no real way to test "PHP Warning" output generated by the following XPath query unless PHPUnit
// converts it to an exception. Unfortunately, this would only apply to tests and not work for production
// executions, which can still flood logs/output unnecessarily. Instead, Emogrifier's error handler should
// always throw an exception and it must be caught here and only rethrown if in debug mode.
try {
// \DOMXPath::query will always return a DOMNodeList or an exception when errors are caught.
$nodesMatchingCssSelectors = $xPath->query($this->translateCssToXpath($cssRule['selector']));
} catch (\InvalidArgumentException $e) {
if ($this->debug) {
throw ($e);
}
continue;
}

Expand Down Expand Up @@ -1535,7 +1542,9 @@ private function getNodesToExclude(\DOMXPath $xPath)
*/
public function handleXpathError($type, $message, $file, $line, array $context)
{
if ($this->debug && $type === E_WARNING && isset($context['cssRule']['selector'])) {
// Do not tie this conditional to debug mode as it causes unnecessary output. See further explanation in
// ::process() where this exception is caught.
if ($type === E_WARNING && isset($context['cssRule']['selector'])) {
throw new \InvalidArgumentException(
sprintf(
'%s in selector >> %s << in %s on line %s',
Expand Down

0 comments on commit 25cfbd0

Please sign in to comment.