Complex classes like Composer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Composer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Composer |
||
|
|||
13 | { |
||
14 | /** |
||
15 | * List of public packages. |
||
16 | * |
||
17 | * @var atring[] |
||
18 | */ |
||
19 | public static $publicPackage = [ |
||
20 | 'yetiforce/csrf-magic' => 'move', |
||
21 | 'ckeditor/ckeditor' => 'move', |
||
22 | ]; |
||
23 | |||
24 | /** |
||
25 | * Copy directories. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | public static $copyDirectories = [ |
||
30 | 'libraries/ckeditor-image-to-base' => 'vendor/ckeditor/ckeditor/plugins/ckeditor-image-to-base' |
||
31 | ]; |
||
32 | |||
33 | /** |
||
34 | * Custom copy. |
||
35 | * |
||
36 | * @param string $rootDir |
||
37 | */ |
||
38 | public static function customCopy(string $rootDir): void |
||
53 | |||
54 | /** |
||
55 | * The function copies files. |
||
56 | * |
||
57 | * @param string $src |
||
58 | * @param string $dest |
||
59 | * |
||
60 | * @return int |
||
61 | */ |
||
62 | public static function recurseCopy(string $src, string $dest): int |
||
82 | |||
83 | /** |
||
84 | * List of redundant files. |
||
85 | * |
||
86 | * @var array |
||
87 | */ |
||
88 | public static $clearFiles = [ |
||
89 | '.github', |
||
90 | '.git', |
||
91 | '.gitattributes', |
||
92 | '.gitignore', |
||
93 | '.gitkeep', |
||
94 | '.editorconfig', |
||
95 | '.styleci.yml', |
||
96 | '.travis.yml', |
||
97 | 'mkdocs.yml', |
||
98 | '.coveralls.yml', |
||
99 | '.scrutinizer.yml', |
||
100 | '.php_cs.dist', |
||
101 | 'build.xml', |
||
102 | 'phpunit.xml.dist', |
||
103 | 'phpunit.xml', |
||
104 | 'changelog.phpexcel.md', |
||
105 | 'changelog.md', |
||
106 | 'changes.md', |
||
107 | 'contributing.md', |
||
108 | 'readme.md', |
||
109 | 'security.md', |
||
110 | 'upgrade.md', |
||
111 | 'docs', |
||
112 | 'demo', |
||
113 | 'examples', |
||
114 | 'extras', |
||
115 | 'install', |
||
116 | 'js-test', |
||
117 | 'maintenance', |
||
118 | 'migrations', |
||
119 | 'news', |
||
120 | 'phorum', |
||
121 | 'readme', |
||
122 | 'sample', |
||
123 | 'samples', |
||
124 | 'todo', |
||
125 | 'test', |
||
126 | 'tests', |
||
127 | 'whatsnew', |
||
128 | 'wysiwyg', |
||
129 | 'views', |
||
130 | '_translationstatus.txt', |
||
131 | 'composer_release_notes.txt', |
||
132 | 'change_log.txt', |
||
133 | 'cldr-version.txt', |
||
134 | 'inheritance_release_notes.txt', |
||
135 | 'metadata-version.txt', |
||
136 | 'modx.txt', |
||
137 | 'news.txt', |
||
138 | 'new_features.txt', |
||
139 | 'readme.txt', |
||
140 | 'smarty_2_bc_notes.txt', |
||
141 | 'smarty_3.0_bc_notes.txt', |
||
142 | 'smarty_3.1_notes.txt', |
||
143 | '.sami.php', |
||
144 | 'get_oauth_token.php', |
||
145 | 'test-settings.sample.php', |
||
146 | 'test-settings.travis.php', |
||
147 | 'install.fr.utf8', |
||
148 | 'jquery.min.js', |
||
149 | 'release1-update.php', |
||
150 | 'release2-tag.php', |
||
151 | 'phpdoc.ini', |
||
152 | 'crowdin.yml', |
||
153 | 'sonar-project.properties', |
||
154 | ]; |
||
155 | |||
156 | public static $clearFilesModule = [ |
||
157 | ]; |
||
158 | |||
159 | /** |
||
160 | * Post update and post install function. |
||
161 | * |
||
162 | * @param \Composer\Script\Event $event |
||
163 | */ |
||
164 | public static function install(\Composer\Script\Event $event) |
||
193 | |||
194 | /** |
||
195 | * Delete redundant files. |
||
196 | */ |
||
197 | public static function clear() |
||
233 | } |
||
234 |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.