Conditions | 2 |
Paths | 2 |
Total Lines | 51 |
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 |
||
28 | public static function handleChangedOrganizationType(ConfigEvent $event) |
||
29 | { |
||
30 | Event::off( |
||
31 | OrganizationType::class, |
||
32 | OrganizationType::EVENT_AFTER_INSERT, |
||
33 | [ |
||
34 | ManageOrganizationTypeProjectConfig::class, |
||
35 | 'save' |
||
36 | ] |
||
37 | ); |
||
38 | |||
39 | Event::off( |
||
40 | OrganizationType::class, |
||
41 | OrganizationType::EVENT_AFTER_UPDATE, |
||
42 | [ |
||
43 | ManageOrganizationTypeProjectConfig::class, |
||
44 | 'save' |
||
45 | ] |
||
46 | ); |
||
47 | |||
48 | // Get the UID that was matched in the config path |
||
49 | $uid = $event->tokenMatches[0]; |
||
50 | |||
51 | if (null === ($provider = OrganizationType::findOne([ |
||
52 | 'uid' => $uid |
||
53 | ]))) { |
||
54 | $provider = new OrganizationType(); |
||
55 | } |
||
56 | |||
57 | Craft::configure($provider, $event->newValue); |
||
|
|||
58 | |||
59 | $provider->save(); |
||
60 | |||
61 | Event::on( |
||
62 | OrganizationType::class, |
||
63 | OrganizationType::EVENT_AFTER_INSERT, |
||
64 | [ |
||
65 | ManageOrganizationTypeProjectConfig::class, |
||
66 | 'save' |
||
67 | ] |
||
68 | ); |
||
69 | |||
70 | Event::on( |
||
71 | OrganizationType::class, |
||
72 | OrganizationType::EVENT_AFTER_UPDATE, |
||
73 | [ |
||
74 | ManageOrganizationTypeProjectConfig::class, |
||
75 | 'save' |
||
76 | ] |
||
77 | ); |
||
78 | } |
||
79 | |||
227 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.