Conditions | 11 |
Paths | 25 |
Total Lines | 50 |
Code Lines | 26 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
48 | public function requireDefaultRecords() |
||
49 | { |
||
50 | if (!UserDefinedForm::config()->get('upgrade_on_build')) { |
||
51 | return; |
||
52 | } |
||
53 | |||
54 | $updated = 0; |
||
55 | foreach ($this->targets as $className => $fieldNames) { |
||
56 | foreach ($fieldNames as $fieldName) { |
||
57 | /** @var DataList $list */ |
||
58 | $list = $className::get(); |
||
59 | |||
60 | foreach ($list as $entry) { |
||
61 | /** @var DataObject $relationshipObject */ |
||
62 | $relationshipObject = Injector::inst()->get($entry->$fieldName); |
||
63 | if (!$relationshipObject) { |
||
64 | continue; |
||
65 | } |
||
66 | |||
67 | // If the defined data class doesn't have the UserForm trait applied, it's probably wrong. Re-map |
||
68 | // it to a default value that does |
||
69 | $classTraits = class_uses($relationshipObject); |
||
70 | if (in_array(UserForm::class, $classTraits)) { |
||
71 | continue; |
||
72 | } |
||
73 | |||
74 | // Don't rewrite class values when an existing value is set and is an instance of UserDefinedForm |
||
75 | if ($relationshipObject instanceof UserDefinedForm) { |
||
76 | continue; |
||
77 | } |
||
78 | |||
79 | $entry->$fieldName = $this->defaultReplacement; |
||
80 | try { |
||
81 | $entry->write(); |
||
82 | $updated++; |
||
83 | } catch (ValidationException $ex) { |
||
84 | // no-op, allow the rest of dev/build to continue. There may be an error indicating that the |
||
85 | // object's class doesn't exist, which can be fixed by {@link DatabaseAdmin::doBuild} and this |
||
86 | // logic will work the next time dev/build is run. |
||
87 | } |
||
88 | } |
||
89 | } |
||
90 | } |
||
91 | |||
92 | if ($updated) { |
||
93 | $message = "Corrected {$updated} default polymorphic class names to {$this->defaultReplacement}"; |
||
94 | if (Director::is_cli()) { |
||
95 | echo sprintf(" * %s\n", $message); |
||
96 | } else { |
||
97 | echo sprintf("<li>%s</li>\n", $message); |
||
98 | } |
||
102 |