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