| Conditions | 1 |
| Paths | 1 |
| Total Lines | 71 |
| Code Lines | 19 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 26 | public function testBuildPipeline() |
||
| 27 | { |
||
| 28 | $analyticsManager = $this->getMockBuilder(AnalyticsManager::class) |
||
| 29 | ->disableOriginalConstructor() |
||
| 30 | ->getMock(); |
||
| 31 | $buildPipeline = $this->getPrivateClassMethod(AnalyticsManager::class, 'buildPipeline'); |
||
| 32 | $mapper = new JsonMapper(); |
||
| 33 | $date = new \DateTime('-4 years'); |
||
| 34 | $year = $date->format('Y'); |
||
| 35 | $expect = '[{"$match":{"created_year":{"$gte":'.$year.'}}},{"$group":{"_id":"app-count","count":{"$sum":1}}}]'; |
||
| 36 | |||
| 37 | $definitionA = json_decode( |
||
| 38 | '{ |
||
| 39 | "collection": "App", |
||
| 40 | "route": "app", |
||
| 41 | "type": "object", |
||
| 42 | "aggregate": { |
||
| 43 | "$match": { |
||
| 44 | "created_year": { |
||
| 45 | "$gte": "PARSE_DATE(-4 years|Y)" |
||
| 46 | } |
||
| 47 | }, |
||
| 48 | "$group": { |
||
| 49 | "_id": "app-count", |
||
| 50 | "count": { |
||
| 51 | "$sum": 1 |
||
| 52 | } |
||
| 53 | } |
||
| 54 | }, |
||
| 55 | "schema": {} |
||
| 56 | } |
||
| 57 | ' |
||
| 58 | ); |
||
| 59 | |||
| 60 | $modelA = $mapper->map($definitionA, new AnalyticModel()); |
||
| 61 | $resultA = json_encode($buildPipeline->invokeArgs($analyticsManager, [$modelA])); |
||
| 62 | $this->assertEquals($expect, $resultA); |
||
| 63 | |||
| 64 | // Pipeline |
||
| 65 | $definitionB = json_decode( |
||
| 66 | '{ |
||
| 67 | "collection": "App", |
||
| 68 | "route": "app", |
||
| 69 | "type": "object", |
||
| 70 | "pipeline": [ |
||
| 71 | { |
||
| 72 | "$match": { |
||
| 73 | "created_year": { |
||
| 74 | "$gte": "PARSE_DATE(-4 years|Y)" |
||
| 75 | } |
||
| 76 | } |
||
| 77 | }, |
||
| 78 | { |
||
| 79 | "$group": { |
||
| 80 | "_id": "app-count", |
||
| 81 | "count": { |
||
| 82 | "$sum": 1 |
||
| 83 | } |
||
| 84 | } |
||
| 85 | } |
||
| 86 | ], |
||
| 87 | "schema": {} |
||
| 88 | } |
||
| 89 | ' |
||
| 90 | ); |
||
| 91 | |||
| 92 | $modelB = $mapper->map($definitionB, new AnalyticModel()); |
||
| 93 | |||
| 94 | $resultB = json_encode($buildPipeline->invokeArgs($analyticsManager, [$modelB])); |
||
| 95 | $this->assertEquals($expect, $resultB); |
||
| 96 | } |
||
| 97 | } |
||
| 98 |