| Conditions | 1 |
| Paths | 1 |
| Total Lines | 188 |
| Code Lines | 100 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 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 |
||
| 22 | public function testExecutesUsingASchema() : void |
||
| 23 | { |
||
| 24 | $BlogSerializableValueType = new CustomScalarType([ |
||
| 25 | 'name' => 'JsonSerializableValueScalar', |
||
| 26 | 'serialize' => static function ($value) { |
||
| 27 | return $value; |
||
| 28 | }, |
||
| 29 | ]); |
||
| 30 | |||
| 31 | $BlogArticle = null; |
||
| 32 | $BlogImage = new ObjectType([ |
||
| 33 | 'name' => 'Image', |
||
| 34 | 'fields' => [ |
||
| 35 | 'url' => ['type' => Type::string()], |
||
| 36 | 'width' => ['type' => Type::int()], |
||
| 37 | 'height' => ['type' => Type::int()], |
||
| 38 | ], |
||
| 39 | ]); |
||
| 40 | |||
| 41 | $BlogAuthor = new ObjectType([ |
||
| 42 | 'name' => 'Author', |
||
| 43 | 'fields' => static function () use (&$BlogArticle, &$BlogImage) { |
||
| 44 | return [ |
||
| 45 | 'id' => ['type' => Type::string()], |
||
| 46 | 'name' => ['type' => Type::string()], |
||
| 47 | 'pic' => [ |
||
| 48 | 'args' => ['width' => ['type' => Type::int()], 'height' => ['type' => Type::int()]], |
||
| 49 | 'type' => $BlogImage, |
||
| 50 | 'resolve' => static function ($obj, $args) { |
||
| 51 | return $obj['pic']($args['width'], $args['height']); |
||
| 52 | }, |
||
| 53 | ], |
||
| 54 | 'recentArticle' => $BlogArticle, |
||
| 55 | ]; |
||
| 56 | }, |
||
| 57 | ]); |
||
| 58 | |||
| 59 | $BlogArticle = new ObjectType([ |
||
| 60 | 'name' => 'Article', |
||
| 61 | 'fields' => [ |
||
| 62 | 'id' => ['type' => Type::nonNull(Type::string())], |
||
| 63 | 'isPublished' => ['type' => Type::boolean()], |
||
| 64 | 'author' => ['type' => $BlogAuthor], |
||
| 65 | 'title' => ['type' => Type::string()], |
||
| 66 | 'body' => ['type' => Type::string()], |
||
| 67 | 'keywords' => ['type' => Type::listOf(Type::string())], |
||
| 68 | 'meta' => ['type' => $BlogSerializableValueType], |
||
| 69 | ], |
||
| 70 | ]); |
||
| 71 | |||
| 72 | $BlogQuery = new ObjectType([ |
||
| 73 | 'name' => 'Query', |
||
| 74 | 'fields' => [ |
||
| 75 | 'article' => [ |
||
| 76 | 'type' => $BlogArticle, |
||
| 77 | 'args' => ['id' => ['type' => Type::id()]], |
||
| 78 | 'resolve' => function ($rootValue, $args) { |
||
| 79 | return $this->article($args['id']); |
||
| 80 | }, |
||
| 81 | ], |
||
| 82 | 'feed' => [ |
||
| 83 | 'type' => Type::listOf($BlogArticle), |
||
| 84 | 'resolve' => function () { |
||
| 85 | return [ |
||
| 86 | $this->article(1), |
||
| 87 | $this->article(2), |
||
| 88 | $this->article(3), |
||
| 89 | $this->article(4), |
||
| 90 | $this->article(5), |
||
| 91 | $this->article(6), |
||
| 92 | $this->article(7), |
||
| 93 | $this->article(8), |
||
| 94 | $this->article(9), |
||
| 95 | $this->article(10), |
||
| 96 | ]; |
||
| 97 | }, |
||
| 98 | ], |
||
| 99 | ], |
||
| 100 | ]); |
||
| 101 | |||
| 102 | $BlogSchema = new Schema(['query' => $BlogQuery]); |
||
| 103 | |||
| 104 | $request = ' |
||
| 105 | { |
||
| 106 | feed { |
||
| 107 | id, |
||
| 108 | title |
||
| 109 | }, |
||
| 110 | article(id: "1") { |
||
| 111 | ...articleFields, |
||
| 112 | author { |
||
| 113 | id, |
||
| 114 | name, |
||
| 115 | pic(width: 640, height: 480) { |
||
| 116 | url, |
||
| 117 | width, |
||
| 118 | height |
||
| 119 | }, |
||
| 120 | recentArticle { |
||
| 121 | ...articleFields, |
||
| 122 | keywords |
||
| 123 | } |
||
| 124 | } |
||
| 125 | meta |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | fragment articleFields on Article { |
||
| 130 | id, |
||
| 131 | isPublished, |
||
| 132 | title, |
||
| 133 | body, |
||
| 134 | hidden, |
||
| 135 | notdefined |
||
| 136 | } |
||
| 137 | '; |
||
| 138 | |||
| 139 | $expected = [ |
||
| 140 | 'data' => [ |
||
| 141 | 'feed' => [ |
||
| 142 | [ |
||
| 143 | 'id' => '1', |
||
| 144 | 'title' => 'My Article 1', |
||
| 145 | ], |
||
| 146 | [ |
||
| 147 | 'id' => '2', |
||
| 148 | 'title' => 'My Article 2', |
||
| 149 | ], |
||
| 150 | [ |
||
| 151 | 'id' => '3', |
||
| 152 | 'title' => 'My Article 3', |
||
| 153 | ], |
||
| 154 | [ |
||
| 155 | 'id' => '4', |
||
| 156 | 'title' => 'My Article 4', |
||
| 157 | ], |
||
| 158 | [ |
||
| 159 | 'id' => '5', |
||
| 160 | 'title' => 'My Article 5', |
||
| 161 | ], |
||
| 162 | [ |
||
| 163 | 'id' => '6', |
||
| 164 | 'title' => 'My Article 6', |
||
| 165 | ], |
||
| 166 | [ |
||
| 167 | 'id' => '7', |
||
| 168 | 'title' => 'My Article 7', |
||
| 169 | ], |
||
| 170 | [ |
||
| 171 | 'id' => '8', |
||
| 172 | 'title' => 'My Article 8', |
||
| 173 | ], |
||
| 174 | [ |
||
| 175 | 'id' => '9', |
||
| 176 | 'title' => 'My Article 9', |
||
| 177 | ], |
||
| 178 | [ |
||
| 179 | 'id' => '10', |
||
| 180 | 'title' => 'My Article 10', |
||
| 181 | ], |
||
| 182 | ], |
||
| 183 | 'article' => [ |
||
| 184 | 'id' => '1', |
||
| 185 | 'isPublished' => true, |
||
| 186 | 'title' => 'My Article 1', |
||
| 187 | 'body' => 'This is a post', |
||
| 188 | 'author' => [ |
||
| 189 | 'id' => '123', |
||
| 190 | 'name' => 'John Smith', |
||
| 191 | 'pic' => [ |
||
| 192 | 'url' => 'cdn://123', |
||
| 193 | 'width' => 640, |
||
| 194 | 'height' => 480, |
||
| 195 | ], |
||
| 196 | 'recentArticle' => [ |
||
| 197 | 'id' => '1', |
||
| 198 | 'isPublished' => true, |
||
| 199 | 'title' => 'My Article 1', |
||
| 200 | 'body' => 'This is a post', |
||
| 201 | 'keywords' => ['foo', 'bar', '1', '1', null], |
||
| 202 | ], |
||
| 203 | ], |
||
| 204 | 'meta' => [ 'title' => 'My Article 1 | My Blog' ], |
||
| 205 | ], |
||
| 206 | ], |
||
| 207 | ]; |
||
| 208 | |||
| 209 | self::assertEquals($expected, Executor::execute($BlogSchema, Parser::parse($request))->toArray()); |
||
| 210 | } |
||
| 248 |