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