| Conditions | 3 |
| Paths | 4 |
| Total Lines | 107 |
| 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 |
||
| 111 | public function testAppendFormFieldElement(): void |
||
| 112 | { |
||
| 113 | $admin = $this->createMock(AdminInterface::class); |
||
| 114 | $admin |
||
| 115 | ->method('getClass') |
||
| 116 | ->willReturn(Foo::class); |
||
| 117 | |||
| 118 | $associationAdmin = $this->createMock(AdminInterface::class); |
||
| 119 | $associationAdmin |
||
| 120 | ->method('getClass') |
||
| 121 | ->willReturn(Bar::class); |
||
| 122 | |||
| 123 | $associationMapping = [ |
||
| 124 | 'fieldName' => 'bar', |
||
| 125 | 'targetEntity' => Foo::class, |
||
| 126 | 'sourceEntity' => Foo::class, |
||
| 127 | 'isOwningSide' => false, |
||
| 128 | ]; |
||
| 129 | |||
| 130 | $fieldDescription = $this->createMock(FieldDescriptionInterface::class); |
||
| 131 | $fieldDescription->method('getAssociationAdmin')->willReturn($associationAdmin); |
||
| 132 | $fieldDescription->method('getAssociationMapping')->willReturn($associationMapping); |
||
| 133 | $fieldDescription->method('getParentAssociationMappings')->willReturn([]); |
||
| 134 | |||
| 135 | $admin |
||
| 136 | ->method('getFormFieldDescription') |
||
| 137 | ->willReturn($fieldDescription); |
||
| 138 | |||
| 139 | $associationAdmin |
||
| 140 | ->method('getFormFieldDescriptions') |
||
| 141 | ->willReturn([ |
||
| 142 | 'bar' => $fieldDescription, |
||
| 143 | ]); |
||
| 144 | |||
| 145 | $request = $this->createMock(Request::class); |
||
| 146 | $request |
||
| 147 | ->method('get') |
||
| 148 | ->willReturn([ |
||
| 149 | 'bar' => [ |
||
| 150 | [ |
||
| 151 | 'baz' => [ |
||
| 152 | 'baz' => true, |
||
| 153 | ], |
||
| 154 | ], |
||
| 155 | ['_delete' => true], |
||
| 156 | ], |
||
| 157 | ]); |
||
| 158 | |||
| 159 | $request->request = new ParameterBag(); |
||
| 160 | |||
| 161 | $admin |
||
| 162 | ->expects($this->atLeastOnce()) |
||
| 163 | ->method('getRequest') |
||
| 164 | ->willReturn($request); |
||
| 165 | |||
| 166 | $foo = $this->createMock(Foo::class); |
||
| 167 | $admin |
||
| 168 | ->method('hasSubject') |
||
| 169 | ->willReturn(true); |
||
| 170 | $admin |
||
| 171 | ->method('getSubject') |
||
| 172 | ->willReturn($foo); |
||
| 173 | |||
| 174 | $bar = new \stdClass(); |
||
| 175 | $associationAdmin |
||
| 176 | ->expects($this->atLeastOnce()) |
||
| 177 | ->method('getNewInstance') |
||
| 178 | ->willReturn($bar); |
||
| 179 | |||
| 180 | $foo->expects($this->atLeastOnce())->method('addBar')->with($bar); |
||
| 181 | |||
| 182 | $dataMapper = $this->createMock(DataMapperInterface::class); |
||
| 183 | $formFactory = $this->createMock(FormFactoryInterface::class); |
||
| 184 | $eventDispatcher = $this->createMock(EventDispatcherInterface::class); |
||
| 185 | $formBuilder = new FormBuilder('test', \get_class($foo), $eventDispatcher, $formFactory); |
||
| 186 | $childFormBuilder = new FormBuilder('bar', \stdClass::class, $eventDispatcher, $formFactory); |
||
| 187 | $childFormBuilder->setCompound(true); |
||
| 188 | $childFormBuilder->setDataMapper($dataMapper); |
||
| 189 | $subChildFormBuilder = new FormBuilder('baz', \stdClass::class, $eventDispatcher, $formFactory); |
||
| 190 | $subChildFormBuilder->setCompound(true); |
||
| 191 | $subChildFormBuilder->setDataMapper($dataMapper); |
||
| 192 | $childFormBuilder->add($subChildFormBuilder); |
||
| 193 | |||
| 194 | $formBuilder->setRequestHandler(new HttpFoundationRequestHandler()); |
||
| 195 | $formBuilder->setCompound(true); |
||
| 196 | $formBuilder->setDataMapper($dataMapper); |
||
| 197 | $formBuilder->add($childFormBuilder); |
||
| 198 | |||
| 199 | $associationAdmin->expects($this->atLeastOnce())->method('setSubject')->with($bar); |
||
| 200 | $admin->method('getFormBuilder')->willReturn($formBuilder); |
||
| 201 | |||
| 202 | $finalForm = $this->helper->appendFormFieldElement($admin, $foo, 'test_bar')[1]; |
||
| 203 | |||
| 204 | foreach ($finalForm->get($childFormBuilder->getName()) as $childField) { |
||
| 205 | $this->assertFalse($childField->has('_delete')); |
||
| 206 | } |
||
| 207 | |||
| 208 | $deleteFormBuilder = new FormBuilder('_delete', null, $eventDispatcher, $formFactory); |
||
| 209 | $subChildFormBuilder->add($deleteFormBuilder, CheckboxType::class, ['delete' => false]); |
||
| 210 | |||
| 211 | $finalForm = $this->helper->appendFormFieldElement($admin, $foo, 'test_bar')[1]; |
||
| 212 | |||
| 213 | foreach ($finalForm->get($childFormBuilder->getName()) as $childField) { |
||
| 214 | $this->assertTrue($childField->has('_delete')); |
||
| 215 | $this->assertSame('', $childField->get('_delete')->getData()); |
||
| 216 | } |
||
| 217 | } |
||
| 218 | |||
| 281 |
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: