Conditions | 15 |
Paths | 4098 |
Total Lines | 83 |
Code Lines | 42 |
Lines | 6 |
Ratio | 7.23 % |
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 |
||
36 | protected function initialize(InputInterface $input, OutputInterface $output) |
||
37 | { |
||
38 | // |
||
39 | // First try to read the configuration file if exists |
||
40 | // |
||
41 | $this->getApplication()->ignoreMissingConfiguration(); |
||
|
|||
42 | $configurationLoader = $this->get('configuration.loader'); |
||
43 | |||
44 | try { |
||
45 | // Init configuration by manually read configuration file (if already exists) |
||
46 | $this->configuration = $configurationLoader->loadConfiguration(); |
||
47 | } catch (ConfigurationLoadingException $e) { |
||
48 | $this->configuration = [ |
||
49 | 'server' => [], |
||
50 | 'project' => null, |
||
51 | 'files' => [], |
||
52 | ]; |
||
53 | } |
||
54 | |||
55 | // |
||
56 | // Then add config from input |
||
57 | // |
||
58 | $server = [ |
||
59 | 'scheme' => null, |
||
60 | 'user' => null, |
||
61 | 'pass' => null, |
||
62 | 'port' => null, |
||
63 | 'host' => null |
||
64 | ]; |
||
65 | |||
66 | if (null !== $url = $input->getArgument('url')) { |
||
67 | $urlParts = parse_url($url); |
||
68 | |||
69 | if (false === $urlParts) { |
||
70 | throw new InvalidArgumentException("$url is not a valid URL"); |
||
71 | } |
||
72 | |||
73 | $server = array_merge($server, $urlParts); |
||
74 | } |
||
75 | |||
76 | if (null !== $hostname = $server['host']) { |
||
77 | $this->configuration['server']['hostname'] = $hostname; |
||
78 | } |
||
79 | |||
80 | if (null !== $scheme = $server['scheme']) { |
||
81 | $this->configuration['server']['use_ssl'] = 'https' === $scheme; |
||
82 | } |
||
83 | |||
84 | if (isset($this->configuration['server']['use_ssl']) && false === $this->configuration['server']['use_ssl']) { |
||
85 | unset($this->configuration['server']['use_ssl']); |
||
86 | } |
||
87 | |||
88 | View Code Duplication | if (null !== $port = $server['port']) { |
|
89 | $this->configuration['server']['port'] = $port; |
||
90 | } |
||
91 | |||
92 | if (null !== $username = $server['user']) { |
||
93 | $this->configuration['server']['username'] = $username; |
||
94 | } |
||
95 | |||
96 | View Code Duplication | if (null !== $password = $server['pass']) { |
|
97 | $this->configuration['server']['password'] = $password; |
||
98 | } |
||
99 | |||
100 | if (null !== $projectSlug = $input->getArgument('project')) { |
||
101 | $this->configuration['project'] = $projectSlug; |
||
102 | } |
||
103 | |||
104 | if (array() !== $patterns = $input->getArgument('pattern')) { |
||
105 | $this->configuration['files'] = $patterns; |
||
106 | } |
||
107 | |||
108 | // If no file are already set, try to find possible ones |
||
109 | if (empty($this->configuration['files'])) { |
||
110 | $inDir = $this->get('configuration.loader')->getRootDirectory(); |
||
111 | $this->configuration['files'] = $this->get('file.pattern_guess')->suggestPatterns($inDir); |
||
112 | } |
||
113 | |||
114 | // If no pattern found, add example of file pattern |
||
115 | if (empty($this->configuration['files'])) { |
||
116 | $this->configuration['files'][] = 'path/to/translations.<locale>.yml'; |
||
117 | } |
||
118 | } |
||
119 | |||
227 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: