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 namespace League\Fractal\Test; |
||
| 12 | class ScopeTest extends \PHPUnit_Framework_TestCase |
||
| 13 | { |
||
| 14 | protected $simpleItem = ['foo' => 'bar']; |
||
| 15 | protected $simpleCollection = [['foo' => 'bar']]; |
||
| 16 | |||
| 17 | public function testEmbedChildScope() |
||
| 18 | { |
||
| 19 | $manager = new Manager(); |
||
| 20 | |||
| 21 | $resource = new Item(['foo' => 'bar'], function () { |
||
| 22 | }); |
||
| 23 | |||
| 24 | $scope = new Scope($manager, $resource, 'book'); |
||
| 25 | $this->assertSame($scope->getScopeIdentifier(), 'book'); |
||
| 26 | $childScope = $scope->embedChildScope('author', $resource); |
||
| 27 | |||
| 28 | $this->assertInstanceOf('League\Fractal\Scope', $childScope); |
||
| 29 | } |
||
| 30 | |||
| 31 | View Code Duplication | public function testGetManager() |
|
| 32 | { |
||
| 33 | $resource = new Item(['foo' => 'bar'], function () { |
||
| 34 | }); |
||
| 35 | |||
| 36 | $scope = new Scope(new Manager(), $resource, 'book'); |
||
| 37 | |||
| 38 | $this->assertInstanceOf('League\Fractal\Manager', $scope->getManager()); |
||
| 39 | } |
||
| 40 | |||
| 41 | View Code Duplication | public function testGetResource() |
|
| 51 | |||
| 52 | /** |
||
| 53 | * @covers League\Fractal\Scope::toArray |
||
| 54 | */ |
||
| 55 | public function testToArray() |
||
| 68 | |||
| 69 | public function testToJson() |
||
| 89 | |||
| 90 | View Code Duplication | public function testGetCurrentScope() |
|
| 106 | |||
| 107 | View Code Duplication | public function testGetIdentifier() |
|
| 123 | |||
| 124 | public function testGetParentScopes() |
||
| 140 | |||
| 141 | public function testIsRequested() |
||
| 142 | { |
||
| 143 | $manager = new Manager(); |
||
| 144 | $manager->parseIncludes(['foo', 'bar', 'baz.bart']); |
||
| 145 | |||
| 146 | $scope = new Scope($manager, Mockery::mock('League\Fractal\Resource\ResourceAbstract')); |
||
| 147 | |||
| 148 | $this->assertTrue($scope->isRequested('foo')); |
||
| 149 | $this->assertTrue($scope->isRequested('bar')); |
||
| 150 | $this->assertTrue($scope->isRequested('baz')); |
||
| 151 | $this->assertTrue($scope->isRequested('baz.bart')); |
||
| 152 | $this->assertFalse($scope->isRequested('nope')); |
||
| 153 | |||
| 154 | $childScope = $scope->embedChildScope('baz', Mockery::mock('League\Fractal\Resource\ResourceAbstract')); |
||
| 155 | $this->assertTrue($childScope->isRequested('bart')); |
||
| 156 | $this->assertFalse($childScope->isRequested('foo')); |
||
| 157 | $this->assertFalse($childScope->isRequested('bar')); |
||
| 158 | $this->assertFalse($childScope->isRequested('baz')); |
||
| 159 | } |
||
| 160 | |||
| 161 | public function testIsExcluded() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @expectedException InvalidArgumentException |
||
| 183 | */ |
||
| 184 | public function testScopeRequiresConcreteImplementation() |
||
| 197 | |||
| 198 | public function testToArrayWithIncludes() |
||
| 216 | |||
| 217 | public function testToArrayWithSideloadedIncludes() |
||
| 218 | { |
||
| 219 | $serializer = Mockery::mock('League\Fractal\Serializer\ArraySerializer')->makePartial(); |
||
| 220 | $serializer->shouldReceive('sideloadIncludes')->andReturn(true); |
||
| 221 | $serializer->shouldReceive('item')->andReturnUsing(function ($key, $data) { |
||
| 222 | return ['data' => $data]; |
||
| 223 | }); |
||
| 224 | $serializer->shouldReceive('includedData')->andReturnUsing(function ($key, $data) { |
||
| 225 | return ['sideloaded' => array_pop($data)]; |
||
| 226 | }); |
||
| 227 | |||
| 228 | $manager = new Manager(); |
||
| 229 | $manager->parseIncludes('book'); |
||
| 230 | $manager->setSerializer($serializer); |
||
| 231 | |||
| 232 | $transformer = Mockery::mock('League\Fractal\TransformerAbstract')->makePartial(); |
||
| 233 | $transformer->shouldReceive('getAvailableIncludes')->twice()->andReturn(['book']); |
||
| 234 | $transformer->shouldReceive('transform')->once()->andReturnUsing(function (array $data) { |
||
| 235 | return $data; |
||
| 236 | }); |
||
| 237 | $transformer->shouldReceive('processIncludedResources')->once()->andReturn(['book' => ['yin' => 'yang']]); |
||
| 238 | |||
| 239 | $resource = new Item(['bar' => 'baz'], $transformer); |
||
| 240 | |||
| 241 | $scope = new Scope($manager, $resource); |
||
| 242 | |||
| 243 | $expected = [ |
||
| 244 | 'data' => ['bar' => 'baz'], |
||
| 245 | 'sideloaded' => ['book' => ['yin' => 'yang']], |
||
| 246 | ]; |
||
| 247 | |||
| 248 | $this->assertSame($expected, $scope->toArray()); |
||
| 249 | } |
||
| 250 | |||
| 251 | public function testPushParentScope() |
||
| 267 | |||
| 268 | public function testRunAppropriateTransformerWithItem() |
||
| 269 | { |
||
| 270 | $manager = new Manager(); |
||
| 271 | |||
| 272 | $transformer = Mockery::mock('League\Fractal\TransformerAbstract'); |
||
| 273 | $transformer->shouldReceive('transform')->once()->andReturn($this->simpleItem); |
||
| 274 | $transformer->shouldReceive('getAvailableIncludes')->once()->andReturn([]); |
||
| 275 | $transformer->shouldReceive('getDefaultIncludes')->once()->andReturn([]); |
||
| 276 | $transformer->shouldReceive('setCurrentScope')->once()->andReturn([]); |
||
| 277 | |||
| 278 | $resource = new Item($this->simpleItem, $transformer); |
||
| 279 | $scope = $manager->createData($resource); |
||
| 280 | |||
| 281 | $this->assertSame(['data' => $this->simpleItem], $scope->toArray()); |
||
| 282 | } |
||
| 283 | |||
| 284 | public function testRunAppropriateTransformerWithCollection() |
||
| 285 | { |
||
| 286 | $manager = new Manager(); |
||
| 287 | |||
| 288 | $transformer = Mockery::mock('League\Fractal\TransformerAbstract'); |
||
| 289 | $transformer->shouldReceive('transform')->once()->andReturn(['foo' => 'bar']); |
||
| 290 | $transformer->shouldReceive('getAvailableIncludes')->once()->andReturn([]); |
||
| 291 | $transformer->shouldReceive('getDefaultIncludes')->once()->andReturn([]); |
||
| 292 | $transformer->shouldReceive('setCurrentScope')->once()->andReturn([]); |
||
| 293 | |||
| 294 | $resource = new Collection([['foo' => 'bar']], $transformer); |
||
| 295 | $scope = $manager->createData($resource); |
||
| 296 | |||
| 297 | $this->assertSame(['data' => [['foo' => 'bar']]], $scope->toArray()); |
||
| 298 | |||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @covers League\Fractal\Scope::executeResourceTransformers |
||
| 303 | * @expectedException InvalidArgumentException |
||
| 304 | * @expectedExceptionMessage Argument $resource should be an instance of League\Fractal\Resource\Item or League\Fractal\Resource\Collection |
||
| 305 | */ |
||
| 306 | public function testCreateDataWithClassFuckKnows() |
||
| 307 | { |
||
| 308 | $manager = new Manager(); |
||
| 309 | |||
| 310 | $transformer = Mockery::mock('League\Fractal\TransformerAbstract')->makePartial(); |
||
| 311 | |||
| 312 | $resource = Mockery::mock('League\Fractal\Resource\ResourceAbstract', [$this->simpleItem, $transformer])->makePartial(); |
||
| 313 | $scope = $manager->createData($resource); |
||
| 314 | $scope->toArray(); |
||
| 315 | } |
||
| 316 | |||
| 317 | public function testPaginatorOutput() |
||
| 371 | |||
| 372 | public function testCursorOutput() |
||
| 409 | |||
| 410 | public function testDefaultIncludeSuccess() |
||
| 411 | { |
||
| 412 | $manager = new Manager(); |
||
| 430 | |||
| 431 | public function tearDown() |
||
| 432 | { |
||
| 433 | Mockery::close(); |
||
| 434 | } |
||
| 435 | } |
||
| 436 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.