Conditions | 14 |
Paths | 67 |
Total Lines | 56 |
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 |
||
20 | public function __invoke($var) |
||
21 | { |
||
22 | $data = file_get_contents($this->path); |
||
23 | if(!$data) { |
||
24 | throw new SerializationFailureException(sprintf('Failed to read file at path %s!', $this->path)); |
||
25 | } |
||
26 | |||
27 | // FIXME: Cache parsed config within normalizer instance |
||
28 | $yaml = Yaml::parse($data); |
||
29 | $keys = array_keys($yaml); |
||
30 | $yaml = $yaml[$keys[0]]; |
||
31 | if(!$yaml) { |
||
32 | throw new SerializationFailureException('No config at '.$this->path); |
||
33 | } |
||
34 | |||
35 | $hasPolicy = array_key_exists('exclusion_policy', $yaml); |
||
36 | if(!$hasPolicy || ($hasPolicy && 'ALL' !== $yaml['exclusion_policy'])) { |
||
37 | throw new SerializationFailureException(sprintf('This serializer supports only ALL, %s given!', $yaml['exclusion_policy'])); |
||
38 | } |
||
39 | |||
40 | $ref = new \ReflectionObject($var); |
||
41 | $result = []; |
||
42 | $properties = array_key_exists('properties', $yaml) ? $yaml['properties'] : []; |
||
43 | foreach($properties as $key => $config) { |
||
44 | if($config['expose'] !== true) { |
||
45 | continue; |
||
46 | } |
||
47 | |||
48 | $name = array_key_exists('serialized_name', $config) ? $config['serialized_name'] : $key; |
||
49 | |||
50 | if($ref->hasProperty($key)) { |
||
51 | $prop = $ref->getProperty($key); |
||
52 | $prop->setAccessible(true); |
||
53 | |||
54 | $result[$name] = $prop->getValue($var); |
||
55 | |||
56 | continue; |
||
57 | } |
||
58 | |||
59 | $method = 'get'.ucfirst($key); |
||
60 | if(method_exists($var, $method)) { |
||
61 | $result[$name] = $var->{$method}(); |
||
62 | |||
63 | continue; |
||
64 | } |
||
65 | |||
66 | // no method or property found, skip current key |
||
67 | } |
||
68 | |||
69 | $virtual = array_key_exists('virtual_properties', $yaml) ? $yaml['virtual_properties'] : []; |
||
70 | foreach($virtual as $method => $config) { |
||
71 | $result[$config['serialized_name']] = $var->{$method}(); |
||
72 | } |
||
73 | |||
74 | return $result; |
||
75 | } |
||
76 | } |
||
77 |