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] Silence purposefully ignored PHP warnings #400

Merged
merged 2 commits into from
Sep 18, 2017
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
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))
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks!



### 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