Complex classes like PackageController 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 PackageController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class PackageController extends CommonController |
||
20 | { |
||
21 | use \hiqdev\yii2\collection\ObjectTrait; |
||
22 | |||
23 | public function getYears() |
||
30 | |||
31 | public function getYear() |
||
35 | |||
36 | public function getLicense() |
||
40 | |||
41 | public function getIssues() |
||
45 | |||
46 | public function getWiki() |
||
50 | |||
51 | public function getKeywords() |
||
55 | |||
56 | public function getFullName() |
||
60 | |||
61 | public function getSource() |
||
65 | |||
66 | public function getVersion() |
||
70 | |||
71 | public function getNamespace() |
||
75 | |||
76 | public static function defaultNamespace($vendor, $package) |
||
80 | |||
81 | public function getSrc() |
||
87 | |||
88 | public function getHomepage() |
||
92 | |||
93 | public function getForum() |
||
97 | |||
98 | public function getLabel() |
||
102 | |||
103 | public function getTitle() |
||
107 | |||
108 | public function getHeadline() |
||
112 | |||
113 | public function getDescription() |
||
117 | |||
118 | public function getRepositoryUrl($file) |
||
126 | |||
127 | /** |
||
128 | * Composer for the moment. |
||
129 | * To be changed to get actual Package Manager. |
||
130 | */ |
||
131 | public function getPackageManager() |
||
135 | |||
136 | public function hasRequireAny($package) |
||
140 | |||
141 | public function hasRequire($package) |
||
147 | |||
148 | public function hasRequireDev($package) |
||
154 | } |
||
155 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.