Completed
Push — master ( 1f0ecb...bcc29d )
by Ivan
04:21
created

SafeXPath   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
c 1
b 0
f 1
lcom 1
cbo 0
dl 0
loc 30
ccs 10
cts 10
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A onQueryError() 0 4 1
A query() 0 14 2
1
<?php
2
3
namespace SP\Crawler;
4
5
use DOMNode;
6
use DOMXPath;
7
use InvalidArgumentException;
8
9
/**
10
 * @author    Ivan Kerin <[email protected]>
11
 * @copyright 2015, Clippings Ltd.
12
 * @license   http://spdx.org/licenses/BSD-3-Clause
13
 */
14
class SafeXPath extends DOMXPath
15
{
16
    private $errors;
17
18 2
    public function onQueryError($errno, $errstrs)
19
    {
20 2
        $this->errors []= $errstrs;
21 2
    }
22
23
    /**
24
     * @param  string          $xpath
25
     * @param  DOMNode|null $scope
26
     * @throws InvalidArgumentException If xpath is not valid
27
     * @return DOMNodeList|false
28
     */
29 3
    public function query($xpath, DOMNode $scope = null, $registerNodeNS = null)
30
    {
31 3
        set_error_handler([$this, 'onQueryError']);
32
33 3
        $result = parent::query($xpath, $scope, $registerNodeNS);
34
35 3
        restore_error_handler();
36
37 3
        if ($this->errors) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->errors of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
38 2
            throw new InvalidArgumentException(sprintf('XPath error for %s (%s)', $xpath, current($this->errors)));
39
        }
40
41 1
        return $result;
42
    }
43
}
44