Conditions | 3 |
Paths | 3 |
Total Lines | 54 |
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 |
||
22 | public function testLoad10k() |
||
23 | { |
||
24 | $time = microtime(true); |
||
|
|||
25 | $postType = new ObjectType([ |
||
26 | 'name' => 'Post', |
||
27 | 'fields' => [ |
||
28 | 'id' => new IdType(), |
||
29 | 'title' => new StringType(), |
||
30 | 'authors' => [ |
||
31 | 'type' => new ListType(new ObjectType([ |
||
32 | 'name' => 'Author', |
||
33 | 'fields' => [ |
||
34 | 'name' => new StringType() |
||
35 | ] |
||
36 | ])) |
||
37 | ], |
||
38 | ] |
||
39 | ]); |
||
40 | |||
41 | $data = []; |
||
42 | for ($i = 1; $i <= 10000; ++$i) { |
||
43 | $authors = []; |
||
44 | while (count($authors) < rand(1, 4)) { |
||
45 | $authors[] = [ |
||
46 | 'name' => 'Author ' . substr(md5(time()), 0, 4) |
||
47 | ]; |
||
48 | } |
||
49 | $data[] = [ |
||
50 | 'id' => $i, |
||
51 | 'title' => 'Title of ' . $i, |
||
52 | 'authors' => $authors, |
||
53 | ]; |
||
54 | } |
||
55 | |||
56 | $p = new Processor(new Schema([ |
||
57 | 'query' => new ObjectType([ |
||
58 | 'name' => 'RootQuery', |
||
59 | 'fields' => [ |
||
60 | 'posts' => [ |
||
61 | 'type' => new ListType($postType), |
||
62 | 'resolve' => function() use ($data) { |
||
63 | return $data; |
||
64 | } |
||
65 | ] |
||
66 | ], |
||
67 | ]), |
||
68 | ])); |
||
69 | return true; |
||
70 | $p->processPayload('{ posts { id, title, authors { name } } }'); |
||
71 | $res = $p->getResponseData(); |
||
72 | echo "Count: " . count($res['data']['posts']) . "\n"; |
||
73 | var_dump($res['data']['posts'][0]); |
||
74 | printf("Test Time: %04f\n", microtime(true) - $time); |
||
75 | } |
||
76 | |||
77 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.