| Conditions | 1 |
| Paths | 1 |
| Total Lines | 60 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 62 | public function queryElementProvider() { |
||
| 63 | |||
| 64 | #0 |
||
| 65 | $provider[] = array( |
||
|
|
|||
| 66 | array(), |
||
| 67 | array(), |
||
| 68 | '[[Foo::bar]]|limit=42|offset=0|mainlabel=', |
||
| 69 | '%5B%5BFoo%3A%3Abar%5D%5D%7Climit%3D42%7Coffset%3D0%7Cmainlabel%3D' |
||
| 70 | ); |
||
| 71 | |||
| 72 | #1 |
||
| 73 | $provider[] = array( |
||
| 74 | array( 'Foobar' => 'DESC' ), |
||
| 75 | array(), |
||
| 76 | '[[Foo::bar]]|limit=42|offset=0|mainlabel=|sort=Foobar|order=desc', |
||
| 77 | '%5B%5BFoo%3A%3Abar%5D%5D%7Climit%3D42%7Coffset%3D0%7Cmainlabel%3D%7Csort%3DFoobar%7Corder%3Ddesc' |
||
| 78 | ); |
||
| 79 | |||
| 80 | #2 |
||
| 81 | $provider[] = array( |
||
| 82 | array( 'Foobar' => 'DESC', 'Foobaz' => 'ASC' ), |
||
| 83 | array(), |
||
| 84 | '[[Foo::bar]]|limit=42|offset=0|mainlabel=|sort=Foobar,Foobaz|order=desc,asc', |
||
| 85 | '%5B%5BFoo%3A%3Abar%5D%5D%7Climit%3D42%7Coffset%3D0%7Cmainlabel%3D%7Csort%3DFoobar%2CFoobaz%7Corder%3Ddesc%2Casc' |
||
| 86 | ); |
||
| 87 | |||
| 88 | #3 |
||
| 89 | $printRequest = $this->getMockBuilder( '\SMW\Query\PrintRequest' ) |
||
| 90 | ->disableOriginalConstructor() |
||
| 91 | ->getMock(); |
||
| 92 | |||
| 93 | $printRequest->expects( $this->any() ) |
||
| 94 | ->method( 'getSerialisation' ) |
||
| 95 | ->will( $this->returnValue( '?ABC' ) ); |
||
| 96 | |||
| 97 | $provider[] = array( |
||
| 98 | array(), |
||
| 99 | array( $printRequest ), |
||
| 100 | '[[Foo::bar]]|?ABC|limit=42|offset=0|mainlabel=', |
||
| 101 | '%5B%5BFoo%3A%3Abar%5D%5D%7C%3FABC%7Climit%3D42%7Coffset%3D0%7Cmainlabel%3D' |
||
| 102 | ); |
||
| 103 | |||
| 104 | #4 (#show returns with an extra =) |
||
| 105 | $printRequest = $this->getMockBuilder( '\SMW\Query\PrintRequest' ) |
||
| 106 | ->disableOriginalConstructor() |
||
| 107 | ->getMock(); |
||
| 108 | |||
| 109 | $printRequest->expects( $this->any() ) |
||
| 110 | ->method( 'getSerialisation' ) |
||
| 111 | ->will( $this->returnValue( '?ABC=' ) ); |
||
| 112 | |||
| 113 | $provider[] = array( |
||
| 114 | array(), |
||
| 115 | array( $printRequest ), |
||
| 116 | '[[Foo::bar]]|?ABC|limit=42|offset=0|mainlabel=', |
||
| 117 | '%5B%5BFoo%3A%3Abar%5D%5D%7C%3FABC%7Climit%3D42%7Coffset%3D0%7Cmainlabel%3D' |
||
| 118 | ); |
||
| 119 | |||
| 120 | return $provider; |
||
| 121 | } |
||
| 122 | |||
| 124 |
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.