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 ActionsMenuBuilderTest extends TestCase |
||
21 | { |
||
22 | /** |
||
23 | * @var ActionsMenuBuilder |
||
24 | */ |
||
25 | protected $builder; |
||
26 | |||
27 | /** |
||
28 | * Sets up the fixture, for example, opens a network connection. |
||
29 | * This method is called before a test is executed. |
||
30 | */ |
||
31 | protected function setUp() |
||
50 | |||
51 | /** |
||
52 | * @throws \Exception |
||
53 | * |
||
54 | * @return \Doctrine\ORM\EntityManager |
||
55 | */ |
||
56 | protected function getMockedEntityManager() |
||
73 | |||
74 | View Code Duplication | public function testCreateSubActionsMenu() |
|
89 | |||
90 | public function testCreateActionsMenuDraft() |
||
91 | { |
||
92 | $nodeTranslation = new NodeTranslation(); |
||
93 | $nodeTranslation->setNode(new Node()); |
||
94 | |||
95 | $nodeVersion = new NodeVersion(); |
||
96 | $nodeVersion->setType('draft'); |
||
97 | $nodeVersion->setNodeTranslation($nodeTranslation); |
||
98 | |||
99 | $this->builder->setActiveNodeVersion($nodeVersion); |
||
100 | |||
101 | $menu = $this->builder->createActionsMenu(); |
||
102 | $this->assertNotNull($menu->getChild('action.saveasdraft')); |
||
103 | $this->assertNull($menu->getChild('action.recopyfromlanguage')); |
||
104 | $this->assertNotNull($menu->getChild('action.publish')); |
||
105 | $this->assertNotNull($menu->getChild('action.preview')); |
||
106 | $this->assertNull($menu->getChild('action.save')); |
||
107 | |||
108 | View Code Duplication | if ((null !== $nodeTranslation->getNode()->getParent() || $nodeTranslation->getNode()->getChildren()->isEmpty())) { |
|
109 | $this->assertNotNull($menu->getChild('action.delete')); |
||
110 | } else { |
||
111 | $this->assertNull($menu->getChild('action.delete')); |
||
112 | } |
||
113 | |||
114 | $this->assertEquals('page-main-actions js-auto-collapse-buttons', $menu->getChildrenAttribute('class')); |
||
115 | } |
||
116 | |||
117 | public function testCreateActionsMenuPublic() |
||
118 | { |
||
119 | $nodeTranslation = new NodeTranslation(); |
||
120 | $nodeTranslation->setNode(new Node()); |
||
121 | |||
122 | $nodeVersion = new NodeVersion(); |
||
123 | $nodeVersion->setType('public'); |
||
124 | $nodeVersion->setNodeTranslation($nodeTranslation); |
||
125 | |||
126 | $this->builder->setActiveNodeVersion($nodeVersion); |
||
127 | |||
128 | $menu = $this->builder->createActionsMenu(); |
||
129 | $this->assertNotNull($menu->getChild('action.save')); |
||
130 | $this->assertNotNull($menu->getChild('action.saveasdraft')); |
||
131 | $this->assertNull($menu->getChild('action.recopyfromlanguage')); |
||
132 | $this->assertNotNull($menu->getChild('action.preview')); |
||
133 | $this->assertNotNull($menu->getChild('action.publish')); |
||
134 | $this->assertNull($menu->getChild('action.unpublish')); |
||
135 | View Code Duplication | if ((null !== $nodeTranslation->getNode()->getParent() || $nodeTranslation->getNode()->getChildren()->isEmpty())) { |
|
136 | $this->assertNotNull($menu->getChild('action.delete')); |
||
137 | } else { |
||
138 | $this->assertNull($menu->getChild('action.delete')); |
||
139 | } |
||
140 | |||
141 | $nodeTranslation->setOnline(true); |
||
142 | $menu = $this->builder->createActionsMenu(); |
||
143 | $this->assertNotNull($menu->getChild('action.save')); |
||
144 | $this->assertNotNull($menu->getChild('action.saveasdraft')); |
||
145 | $this->assertNull($menu->getChild('action.recopyfromlanguage')); |
||
146 | $this->assertNotNull($menu->getChild('action.preview')); |
||
147 | $this->assertNull($menu->getChild('action.publish')); |
||
148 | $this->assertNotNull($menu->getChild('action.unpublish')); |
||
149 | View Code Duplication | if ((null !== $nodeTranslation->getNode()->getParent() || $nodeTranslation->getNode()->getChildren()->isEmpty())) { |
|
150 | $this->assertNotNull($menu->getChild('action.delete')); |
||
151 | } else { |
||
152 | $this->assertNull($menu->getChild('action.delete')); |
||
153 | } |
||
154 | |||
155 | $this->assertEquals('page-main-actions js-auto-collapse-buttons', $menu->getChildrenAttribute('class')); |
||
156 | } |
||
157 | |||
158 | public function testCreateActionsMenuNonEditable() |
||
181 | |||
182 | View Code Duplication | public function testCreateTopActionsMenu() |
|
196 | |||
197 | public function testSetGetActiveNodeVersion() |
||
203 | |||
204 | public function testShouldShowDeleteButtonWhenTheNodeHasAParent() |
||
222 | |||
223 | public function testShouldShowRecopyButtonWhenTheNodeHasTranslations() |
||
253 | } |
||
254 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: