exampleCallback()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * Matching Text Content.
4
 *
5
 * This example shows one way of matching text content.
6
 * The `:contains()` pseudo-class requires that the ENTIRE CONTENTS
7
 * of an element match exactly. But sometimes what we want is a way
8
 * to match just part of the contents of an element. This example
9
 * illustrates how to accomplish this with a filter callback.
10
 *
11
 * As of QueryPath 2.1beta2, `:contains()` performs a substring match instead of 
12
 * and exact match, so the method outline below is roughly the same as merely
13
 * using `:contains(Release)`.
14
 *
15
 * 
16
 * @author M Butcher <[email protected]>
17
 * @license LGPL The GNU Lesser GPL (LGPL) or an MIT-like license.
18
 */
19
20
/** Include QueryPath. */
21
require_once '../src/QueryPath/QueryPath.php';
22
23
/**
24
 * Check if the string 'Release' is in the text content of any matched nodes.
25
 * 
26
 * Returns TRUE if the text is found, FALSE otherwise. Anytime a filter callback
27
 * returns FALSE, QueryPath will remove it from the matches.
28
 *
29
 * Note that $item is a DOMNode (actually, a DOMElement). So if we wanted to do QueryPath
30
 * manipulations on it, we could wrap it in a `qp()`.
31
 */
32
function exampleCallback($index, $item) {
0 ignored issues
show
Unused Code introduced by
The parameter $index is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

32
function exampleCallback(/** @scrutinizer ignore-unused */ $index, $item) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
33
  $text = qp($item)->text();
34
  return strpos($text, 'Release') !== FALSE;
0 ignored issues
show
Bug introduced by
It seems like $text can also be of type QueryPath\DOMQuery; however, parameter $haystack of strpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
  return strpos(/** @scrutinizer ignore-type */ $text, 'Release') !== FALSE;
Loading history...
35
}
36
37
/*
38
 * This is the QueryPath call.
39
 *
40
 * First we fetch the remote page, parse it, and grab just the `a` tags inside of the summary.
41
 * Then we filter the results through our callback.
42
 * Finally, we fetch all of the matching text and print it.
43
 *
44
 * NOTE: If you are using PHP 5.3, you can define the callback inline instead of separating it
45
 * into a stand-alone function.
46
 */
47
print htmlqp('http://php.net/', 'h1.summary a')
48
  ->filterCallback('exampleCallback')
49
  ->textImplode(PHP_EOL);
50
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...