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 SMW\Tests\PropertyAnnotator; |
||
4 | |||
5 | use SMW\MediaWiki\RedirectTargetFinder; |
||
6 | use SMW\PropertyAnnotator\NullPropertyAnnotator; |
||
7 | use SMW\PropertyAnnotator\RedirectPropertyAnnotator; |
||
8 | use SMW\Tests\Utils\UtilityFactory; |
||
9 | |||
10 | /** |
||
11 | * @covers \SMW\PropertyAnnotator\RedirectPropertyAnnotator |
||
12 | * @group semantic-mediawiki |
||
13 | * |
||
14 | * @license GNU GPL v2+ |
||
15 | * @since 1.9 |
||
16 | * |
||
17 | * @author mwjames |
||
18 | */ |
||
19 | class RedirectPropertyAnnotatorTest extends \PHPUnit_Framework_TestCase { |
||
20 | |||
21 | private $semanticDataFactory; |
||
22 | private $semanticDataValidator; |
||
23 | |||
24 | protected function setUp() { |
||
25 | parent::setUp(); |
||
26 | |||
27 | $this->semanticDataFactory = UtilityFactory::getInstance()->newSemanticDataFactory(); |
||
28 | $this->semanticDataValidator = UtilityFactory::getInstance()->newValidatorFactory()->newSemanticDataValidator(); |
||
29 | } |
||
30 | |||
31 | public function testCanConstruct() { |
||
32 | |||
33 | $semanticData = $this->getMockBuilder( '\SMW\SemanticData' ) |
||
34 | ->disableOriginalConstructor() |
||
35 | ->getMock(); |
||
36 | |||
37 | $redirectTargetFinder = $this->getMockBuilder( '\SMW\MediaWiki\RedirectTargetFinder' ) |
||
38 | ->disableOriginalConstructor() |
||
39 | ->getMock(); |
||
40 | |||
41 | $instance = new RedirectPropertyAnnotator( |
||
42 | new NullPropertyAnnotator( $semanticData ), |
||
43 | $redirectTargetFinder |
||
44 | ); |
||
45 | |||
46 | $this->assertInstanceOf( |
||
47 | '\SMW\PropertyAnnotator\RedirectPropertyAnnotator', |
||
48 | $instance |
||
49 | ); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @dataProvider redirectsDataProvider |
||
54 | */ |
||
55 | public function testAddAnnotation( array $parameter, array $expected ) { |
||
56 | |||
57 | $semanticData = $this->semanticDataFactory->newEmptySemanticData( __METHOD__ ); |
||
58 | |||
59 | $redirectTargetFinder = new RedirectTargetFinder(); |
||
60 | |||
61 | $instance = new RedirectPropertyAnnotator( |
||
62 | new NullPropertyAnnotator( $semanticData ), |
||
63 | $redirectTargetFinder->findRedirectTargetFromText( $parameter['text'] ) |
||
64 | ); |
||
65 | |||
66 | $instance->addAnnotation(); |
||
67 | |||
68 | $this->semanticDataValidator->assertThatPropertiesAreSet( |
||
69 | $expected, |
||
70 | $instance->getSemanticData() |
||
71 | ); |
||
72 | } |
||
73 | |||
74 | public function redirectsDataProvider() { |
||
75 | |||
76 | // #0 Free text |
||
77 | $provider[] = array( |
||
0 ignored issues
–
show
|
|||
78 | array( 'text' => '#REDIRECT [[:Lala]]' ), |
||
79 | array( |
||
80 | 'propertyCount' => 1, |
||
81 | 'propertyKeys' => '_REDI', |
||
82 | 'propertyValues' => ':Lala' |
||
83 | ) |
||
84 | ); |
||
85 | |||
86 | // #1 Free text |
||
87 | $provider[] = array( |
||
88 | array( 'text' => '#REDIRECT [[Lala]]' ), |
||
89 | array( |
||
90 | 'propertyCount' => 1, |
||
91 | 'propertyKeys' => '_REDI', |
||
92 | 'propertyValues' => ':Lala' |
||
93 | ) |
||
94 | ); |
||
95 | |||
96 | |||
97 | // #2 Invalid free text |
||
98 | $provider[] = array( |
||
99 | array( 'text' => '#REDIR [[:Lala]]' ), |
||
100 | array( |
||
101 | 'propertyCount' => 0, |
||
102 | ) |
||
103 | ); |
||
104 | |||
105 | // #3 Empty |
||
106 | $provider[] = array( |
||
107 | array( 'text' => '' ), |
||
108 | array( |
||
109 | 'propertyCount' => 0, |
||
110 | ) |
||
111 | ); |
||
112 | |||
113 | return $provider; |
||
114 | } |
||
115 | |||
116 | } |
||
117 |
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.