| Conditions | 1 |
| Paths | 1 |
| Total Lines | 103 |
| 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 namespace League\Fractal\Test\Resource; |
||
| 17 | public function testSerializingItemResource() |
||
| 18 | { |
||
| 19 | $manager = new Manager(); |
||
| 20 | $manager->parseIncludes('author'); |
||
| 21 | $manager->setSerializer(new DataArraySerializer()); |
||
| 22 | |||
| 23 | $phil = Person::make('Miguel de Cervantes'); |
||
| 24 | $bookData = Book::make('Don Quixote', '1605', $phil); |
||
| 25 | |||
| 26 | // Try without metadata |
||
| 27 | $resource = new Item($bookData, new GenericBookTransformer(), 'book'); |
||
| 28 | $scope = new Scope($manager, $resource); |
||
| 29 | |||
| 30 | $expected = [ |
||
| 31 | 'data' => [ |
||
| 32 | 'title' => 'Don Quixote', |
||
| 33 | 'year' => 1605, |
||
| 34 | 'author' => [ |
||
| 35 | 'data' => [ |
||
| 36 | 'name' => 'Miguel de Cervantes', |
||
| 37 | ], |
||
| 38 | ], |
||
| 39 | ], |
||
| 40 | ]; |
||
| 41 | |||
| 42 | $this->assertSame($expected, $scope->toArray()); |
||
| 43 | |||
| 44 | //Test single field |
||
| 45 | $manager->parseFieldsets(['book' => 'title']); |
||
| 46 | $expected = [ |
||
| 47 | 'data' => [ |
||
| 48 | 'title' => 'Don Quixote', |
||
| 49 | ] |
||
| 50 | ]; |
||
| 51 | $this->assertSame($expected, $scope->toArray()); |
||
| 52 | |||
| 53 | //Test multiple field |
||
| 54 | $manager->parseFieldsets(['book' => 'title,year']); |
||
| 55 | $expected = [ |
||
| 56 | 'data' => [ |
||
| 57 | 'title' => 'Don Quixote', |
||
| 58 | 'year' => 1605 |
||
| 59 | ] |
||
| 60 | ]; |
||
| 61 | $this->assertSame($expected, $scope->toArray()); |
||
| 62 | |||
| 63 | //Test with relationship field |
||
| 64 | $manager->parseFieldsets(['book' => 'title,author', 'author' => 'name']); |
||
| 65 | $expected = [ |
||
| 66 | 'data' => [ |
||
| 67 | 'title' => 'Don Quixote', |
||
| 68 | 'author' => [ |
||
| 69 | 'data' => [ |
||
| 70 | 'name' => 'Miguel de Cervantes' |
||
| 71 | ] |
||
| 72 | ] |
||
| 73 | ] |
||
| 74 | ]; |
||
| 75 | $this->assertSame($expected, $scope->toArray()); |
||
| 76 | |||
| 77 | //Clear all sparse fieldsets |
||
| 78 | $manager->parseFieldsets([]); |
||
| 79 | |||
| 80 | // Same again with metadata |
||
| 81 | $resource = new Item($bookData, new GenericBookTransformer(), 'book'); |
||
| 82 | $resource->setMetaValue('foo', 'bar'); |
||
| 83 | |||
| 84 | $scope = new Scope($manager, $resource); |
||
| 85 | |||
| 86 | $expected = [ |
||
| 87 | 'data' => [ |
||
| 88 | 'title' => 'Don Quixote', |
||
| 89 | 'year' => 1605, |
||
| 90 | 'author' => [ |
||
| 91 | 'data' => [ |
||
| 92 | 'name' => 'Miguel de Cervantes', |
||
| 93 | ], |
||
| 94 | ], |
||
| 95 | ], |
||
| 96 | 'meta' => [ |
||
| 97 | 'foo' => 'bar', |
||
| 98 | ], |
||
| 99 | ]; |
||
| 100 | |||
| 101 | $this->assertSame($expected, $scope->toArray()); |
||
| 102 | |||
| 103 | //Test with relationship field |
||
| 104 | $manager->parseFieldsets(['book' => 'title,author', 'author' => 'name']); |
||
| 105 | $expected = [ |
||
| 106 | 'data' => [ |
||
| 107 | 'title' => 'Don Quixote', |
||
| 108 | 'author' => [ |
||
| 109 | 'data' => [ |
||
| 110 | 'name' => 'Miguel de Cervantes', |
||
| 111 | ] |
||
| 112 | ] |
||
| 113 | ], |
||
| 114 | 'meta' => [ |
||
| 115 | 'foo' => 'bar' |
||
| 116 | ], |
||
| 117 | ]; |
||
| 118 | $this->assertSame($expected, $scope->toArray()); |
||
| 119 | } |
||
| 120 | |||
| 347 |