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 |
||
| 20 | class RequestListenerTest extends EventListenerTest |
||
| 21 | { |
||
| 22 | const REST_ROUTE = '/api/ezp/v2/rest-route'; |
||
| 23 | const NON_REST_ROUTE = '/non-rest-route'; |
||
| 24 | |||
| 25 | public function provideExpectedSubscribedEventTypes() |
||
| 33 | |||
| 34 | View Code Duplication | public static function restRequestUrisProvider() |
|
| 47 | |||
| 48 | View Code Duplication | public static function nonRestRequestsUrisProvider() |
|
| 61 | |||
| 62 | public function testOnKernelRequestNotMasterRequest() |
||
| 63 | { |
||
| 64 | $request = $this->performFakeRequest(self::REST_ROUTE, HttpKernelInterface::SUB_REQUEST); |
||
| 65 | |||
| 66 | self::assertTrue($request->attributes->get('is_rest_request')); |
||
| 67 | } |
||
| 68 | |||
| 69 | public function testOnKernelRequestNotRestRequest() |
||
| 70 | { |
||
| 71 | $request = $this->performFakeRequest(self::NON_REST_ROUTE); |
||
| 72 | |||
| 73 | self::assertFalse($request->attributes->get('is_rest_request')); |
||
| 74 | } |
||
| 75 | |||
| 76 | public function testOnKernelRequestRestRequest() |
||
| 77 | { |
||
| 78 | $request = $this->performFakeRequest(self::REST_ROUTE); |
||
| 79 | |||
| 80 | self::assertTrue($request->attributes->get('is_rest_request')); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @dataProvider restRequestUrisProvider |
||
| 85 | */ |
||
| 86 | public function testRestRequestVariations($uri) |
||
| 87 | { |
||
| 88 | $request = $this->performFakeRequest($uri); |
||
| 89 | |||
| 90 | self::assertTrue($request->attributes->get('is_rest_request')); |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @dataProvider nonRestRequestsUrisProvider |
||
| 95 | */ |
||
| 96 | public function testNonRestRequestVariations($uri) |
||
| 97 | { |
||
| 98 | $request = $this->performFakeRequest($uri); |
||
| 99 | |||
| 100 | self::assertFalse($request->attributes->get('is_rest_request')); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @return RequestListener |
||
| 105 | */ |
||
| 106 | protected function getEventListener() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @return AcceptHeaderVisitorDispatcher|\PHPUnit_Framework_MockObject_MockObject |
||
| 115 | */ |
||
| 116 | public function getVisitorDispatcherMock() |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @return Request |
||
| 125 | */ |
||
| 126 | protected function performFakeRequest($uri, $type = HttpKernelInterface::MASTER_REQUEST) |
||
| 138 | } |
||
| 139 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.