Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 18 | abstract class ValueObjectVisitorBaseTest extends Tests\BaseTest |
||
| 19 | { |
||
| 20 | use AssertXmlTagTrait; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Visitor mock. |
||
| 24 | * |
||
| 25 | * @var \eZ\Publish\Core\REST\Common\Output\Visitor |
||
| 26 | */ |
||
| 27 | protected $visitorMock; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Output generator. |
||
| 31 | * |
||
| 32 | * @var \eZ\Publish\Core\REST\Common\Output\Generator\Xml |
||
| 33 | */ |
||
| 34 | protected $generator; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var \eZ\Publish\Core\REST\Common\RequestParser |
||
| 38 | */ |
||
| 39 | protected $requestParser; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var \Symfony\Component\Routing\RouterInterface|\PHPUnit_Framework_MockObject_MockObject |
||
| 43 | */ |
||
| 44 | private $routerMock; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var \Symfony\Component\Routing\RouterInterface|\PHPUnit_Framework_MockObject_MockObject |
||
| 48 | */ |
||
| 49 | private $templatedRouterMock; |
||
| 50 | |||
| 51 | /** @var int */ |
||
| 52 | private $routerCallIndex = 0; |
||
| 53 | |||
| 54 | /** @var int */ |
||
| 55 | private $templatedRouterCallIndex = 0; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Gets the visitor mock. |
||
| 59 | * |
||
| 60 | * @return \eZ\Publish\Core\REST\Common\Output\Visitor|\PHPUnit_Framework_MockObject_MockObject |
||
| 61 | */ |
||
| 62 | View Code Duplication | protected function getVisitorMock() |
|
| 81 | |||
| 82 | /** |
||
| 83 | * @return \Symfony\Component\HttpFoundation\Response|\PHPUnit_Framework_MockObject_MockObject |
||
| 84 | */ |
||
| 85 | protected function getResponseMock() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Gets the output generator. |
||
| 96 | * |
||
| 97 | * @return \eZ\Publish\Core\REST\Common\Output\Generator\Xml |
||
| 98 | */ |
||
| 99 | protected function getGenerator() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Asserts that the given $xpathExpression returns a non empty node set |
||
| 112 | * with $domNode as its context. |
||
| 113 | * |
||
| 114 | * This method asserts that $xpathExpression results in a non-empty node |
||
| 115 | * set in context of $domNode, by wrapping the "boolean()" function around |
||
| 116 | * it and evaluating it on the document owning $domNode. |
||
| 117 | * |
||
| 118 | * @param \DOMNode $domNode |
||
| 119 | * @param string $xpathExpression |
||
| 120 | */ |
||
| 121 | protected function assertXPath(\DOMNode $domNode, $xpathExpression) |
||
| 134 | |||
| 135 | protected function getVisitor() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @return \eZ\Publish\Core\REST\Common\RequestParser|\PHPUnit_Framework_MockObject_MockObject |
||
| 147 | */ |
||
| 148 | protected function getRequestParser() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @return \Symfony\Component\Routing\RouterInterface|\PHPUnit_Framework_MockObject_MockObject |
||
| 159 | */ |
||
| 160 | protected function getRouterMock() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Resets the router mock and its expected calls index & list. |
||
| 171 | */ |
||
| 172 | protected function resetRouterMock() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Adds an expectation to the routerMock. Expectations must be added sequentially. |
||
| 180 | * |
||
| 181 | * @param string $routeName |
||
| 182 | * @param array $arguments |
||
| 183 | * @param string $returnValue |
||
| 184 | */ |
||
| 185 | View Code Duplication | protected function addRouteExpectation($routeName, $arguments, $returnValue) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Adds the expectated value object visit calls sequence. |
||
| 199 | * |
||
| 200 | * @param array $valueObjects Array of visited value object, or of phpunit matcher ($this->instanceOf(), etc...) . |
||
| 201 | */ |
||
| 202 | protected function setVisitValueObjectExpectations(array $valueObjects) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @return \Symfony\Component\Routing\RouterInterface|\PHPUnit_Framework_MockObject_MockObject |
||
| 219 | */ |
||
| 220 | protected function getTemplatedRouterMock() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Adds an expectation to the templatedRouterMock. Expectations must be added sequentially. |
||
| 231 | * |
||
| 232 | * @param string $routeName |
||
| 233 | * @param array $arguments |
||
| 234 | * @param string $returnValue |
||
| 235 | */ |
||
| 236 | View Code Duplication | protected function addTemplatedRouteExpectation($routeName, $arguments, $returnValue) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Must return an instance of the tested visitor object. |
||
| 250 | * |
||
| 251 | * @return \eZ\Publish\Core\REST\Common\Output\ValueObjectVisitor |
||
| 252 | */ |
||
| 253 | abstract protected function internalGetVisitor(); |
||
| 254 | } |
||
| 255 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: