Driver_Simple_Xpath::find()   A
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.0342

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 8
cts 9
cp 0.8889
rs 9.4222
c 0
b 0
f 0
cc 5
nc 7
nop 2
crap 5.0342
1
<?php
2
3
namespace Openbuildings\Spiderling;
4
5
/**
6
 * Xpath extension for simple driver
7
 *
8
 * @package    Openbuildings\Spiderling
9
 * @author     Ivan Kerin
10
 * @copyright  (c) 2013 OpenBuildings Ltd.
11
 * @license    http://spdx.org/licenses/BSD-3-Clause
12
 */
13
class Driver_Simple_Xpath extends \DOMXpath
14
{
15
	/**
16
	 * Find a DOMElement wit ha given xpath expression, optionally provide parent DOMNode to use as context.
17
	 * @param  string $expression
18
	 * @param  DOMNode $contextnode
19
	 * @return DOMNode
20
	 * @throws Exception_Xpath If no elements were found
21
	 */
22 15
	public function find($expression, $contextnode = NULL)
23
	{
24 15
		if ($expression instanceof \DOMNode)
25 4
			return $expression;
26
27 15
		@ $items = $contextnode ? $this->query($expression, $contextnode) : $this->query($expression);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
28
29 15
		if ( ! $items)
30
			throw new Exception_Xpath('Error in expression: ":expression"', array(':expression' => $expression));
31
32 15
		if ($items->length == 0)
33 1
			throw new Exception_Xpath('No element for selector ":expression"', array(':expression' => $expression));
34
35 15
		return $items->item(0);
36
37
	}
38
}
39