These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace SEQL\Tests; |
||
4 | |||
5 | use SEQL\EmbeddedLinksReplacer; |
||
6 | |||
7 | /** |
||
8 | * @covers \SEQL\EmbeddedLinksReplacer |
||
9 | * @group semantic-external-query-lookup |
||
10 | * |
||
11 | * @license GNU GPL v2+ |
||
12 | * @since 1.0 |
||
13 | * |
||
14 | * @author mwjames |
||
15 | */ |
||
16 | class EmbeddedLinksReplacerTest extends \PHPUnit_Framework_TestCase { |
||
17 | |||
18 | private $querySource; |
||
19 | |||
20 | protected function setUp() { |
||
21 | $this->querySource = 'abc'; |
||
22 | } |
||
23 | |||
24 | public function testCanConstruct() { |
||
25 | |||
26 | $this->assertInstanceOf( |
||
27 | '\SEQL\EmbeddedLinksReplacer', |
||
28 | new EmbeddedLinksReplacer( $this->querySource ) |
||
29 | ); |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * @dataProvider textProvider |
||
34 | */ |
||
35 | public function testReplace( $text, $expected ) { |
||
36 | |||
37 | $instance = new EmbeddedLinksReplacer( $this->querySource ); |
||
38 | |||
39 | $this->assertEquals( |
||
40 | $expected, |
||
41 | $instance->replace( $text ) |
||
42 | ); |
||
43 | } |
||
44 | |||
45 | public function textProvider() { |
||
46 | |||
47 | #0 |
||
48 | $provider[] = array( |
||
0 ignored issues
–
show
|
|||
49 | 'Foo bar', |
||
50 | 'Foo bar' |
||
51 | ); |
||
52 | |||
53 | #1 |
||
54 | $provider[] = array( |
||
55 | 'Foo [42] bar', |
||
56 | 'Foo [42] bar' |
||
57 | ); |
||
58 | |||
59 | #2 |
||
60 | $provider[] = array( |
||
61 | 'Foo [42 1001] bar', |
||
62 | 'Foo [42 1001] bar' |
||
63 | ); |
||
64 | |||
65 | #3 |
||
66 | $provider[] = array( |
||
67 | 'Foo [[42]] bar', |
||
68 | 'Foo [[abc:42|42]] bar' |
||
69 | ); |
||
70 | |||
71 | #4 |
||
72 | $provider[] = array( |
||
73 | 'Foo [[42|1001]] bar', |
||
74 | 'Foo [[abc:42|1001]] bar' |
||
75 | ); |
||
76 | |||
77 | // We can't guess the type of a remote annotation therefore it is turned |
||
78 | // into an simple text value |
||
79 | |||
80 | #5 |
||
81 | $provider[] = array( |
||
82 | 'Foo [[Has number::42]] bar', |
||
83 | 'Foo 42 bar' |
||
84 | ); |
||
85 | |||
86 | #6 |
||
87 | $provider[] = array( |
||
88 | 'Foo [[Has number::42|1001]] bar', |
||
89 | 'Foo 1001 bar' |
||
90 | ); |
||
91 | |||
92 | return $provider; |
||
93 | } |
||
94 | |||
95 | } |
||
96 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.