Conditions | 1 |
Paths | 1 |
Total Lines | 59 |
Code Lines | 42 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
49 | public function testAddToUpdateList() { |
||
50 | |||
51 | $idTable = $this->getMockBuilder( '\stdClass' ) |
||
52 | ->setMethods( array( 'getIDFor' ) ) |
||
53 | ->getMock(); |
||
54 | |||
55 | $idTable->expects( $this->any() ) |
||
56 | ->method( 'getIDFor' ) |
||
57 | ->will( $this->onConsecutiveCalls( 1001 ) ); |
||
58 | |||
59 | $connection = $this->getMockBuilder( '\SMW\MediaWiki\Database' ) |
||
60 | ->disableOriginalConstructor() |
||
61 | ->getMock(); |
||
62 | |||
63 | $connection->expects( $this->once() ) |
||
64 | ->method( 'delete' ) |
||
65 | ->with( |
||
66 | $this->equalTo( \SMWSQLStore3::QUERY_LINKS_TABLE ), |
||
67 | $this->equalTo( array( 's_id' => 42 ) ) ); |
||
68 | |||
69 | $insert[] = array( |
||
|
|||
70 | 's_id' => 42, |
||
71 | 'o_id' => 1001 |
||
72 | ); |
||
73 | |||
74 | $connection->expects( $this->once() ) |
||
75 | ->method( 'insert' ) |
||
76 | ->with( |
||
77 | $this->equalTo( \SMWSQLStore3::QUERY_LINKS_TABLE ), |
||
78 | $this->equalTo( $insert ) ); |
||
79 | |||
80 | $connectionManager = $this->getMockBuilder( '\SMW\ConnectionManager' ) |
||
81 | ->disableOriginalConstructor() |
||
82 | ->getMock(); |
||
83 | |||
84 | $connectionManager->expects( $this->any() ) |
||
85 | ->method( 'getConnection' ) |
||
86 | ->will( $this->returnValue( $connection ) ); |
||
87 | |||
88 | $store = $this->getMockBuilder( '\SMW\SQLStore\SQLStore' ) |
||
89 | ->disableOriginalConstructor() |
||
90 | ->setMethods( array( 'getObjectIds' ) ) |
||
91 | ->getMockForAbstractClass(); |
||
92 | |||
93 | $store->setConnectionManager( $connectionManager ); |
||
94 | |||
95 | $store->expects( $this->any() ) |
||
96 | ->method( 'getObjectIds' ) |
||
97 | ->will( $this->returnValue( $idTable ) ); |
||
98 | |||
99 | $instance = new DependencyLinksTableUpdater( |
||
100 | $store |
||
101 | ); |
||
102 | |||
103 | $instance->clear(); |
||
104 | |||
105 | $instance->addToUpdateList( 42, array( DIWikiPage::newFromText( 'Bar' ) ) ); |
||
106 | $instance->doUpdate(); |
||
107 | } |
||
108 | |||
207 |
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.