Conditions | 4 |
Paths | 4 |
Total Lines | 62 |
Code Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Changes | 8 | ||
Bugs | 5 | Features | 3 |
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 |
||
96 | public function prepare() |
||
97 | { |
||
98 | // Perform this migration and execute only once |
||
99 | if ($this->migrator() != 40) { |
||
100 | // Perform SQL table creation |
||
101 | $path = __DIR__ . '/../sql/'; |
||
102 | foreach (array_slice(scandir($path), 2) as $file) { |
||
103 | $this->database->execute($this->readSQL($path . $file, $this->tablePrefix)); |
||
104 | } |
||
105 | $this->migrator(40); |
||
106 | } |
||
107 | |||
108 | // Initiate migration mechanism |
||
109 | $this->database->migration(get_class($this), array($this, 'migrator')); |
||
110 | |||
111 | // Define permanent table relations |
||
112 | new TableRelation('material', 'user', 'UserID', 0, 'user_id'); |
||
113 | new TableRelation('material', 'gallery', 'MaterialID', TableRelation::T_ONE_TO_MANY); |
||
114 | new TableRelation('material', 'materialfield', 'MaterialID', TableRelation::T_ONE_TO_MANY); |
||
115 | new TableRelation('material', 'field', 'materialfield.FieldID', TableRelation::T_ONE_TO_MANY); |
||
116 | new TableRelation('material', 'structurematerial', 'MaterialID', TableRelation::T_ONE_TO_MANY); |
||
117 | new TableRelation('material', 'structure', 'structurematerial.StructureID', TableRelation::T_ONE_TO_MANY); |
||
118 | new TableRelation('materialfield', 'field', 'FieldID'); |
||
119 | new TableRelation('materialfield', 'material', 'MaterialID'); |
||
120 | new TableRelation('structurematerial', 'structure', 'StructureID'); |
||
121 | new TableRelation('structurematerial', 'materialfield', 'MaterialID', TableRelation::T_ONE_TO_MANY); |
||
122 | new TableRelation('structurematerial', 'material', 'MaterialID', TableRelation::T_ONE_TO_MANY); |
||
123 | new TableRelation('structure', 'material', 'structurematerial.MaterialID', TableRelation::T_ONE_TO_MANY, null, 'manymaterials'); |
||
124 | new TableRelation('structure', 'gallery', 'structurematerial.MaterialID', TableRelation::T_ONE_TO_MANY, null, 'manymaterials'); |
||
125 | /*new TableRelation( 'structure', 'material', 'MaterialID' );*/ |
||
126 | new TableRelation('structure', 'user', 'UserID', 0, 'user_id'); |
||
127 | new TableRelation('structure', 'materialfield', 'material.MaterialID', TableRelation::T_ONE_TO_MANY, 'MaterialID', '_mf'); |
||
128 | new TableRelation('structure', 'structurematerial', 'StructureID', TableRelation::T_ONE_TO_MANY); |
||
129 | new TableRelation('related_materials', 'material', 'first_material', TableRelation::T_ONE_TO_MANY, 'MaterialID'); |
||
130 | new TableRelation('related_materials', 'materialfield', 'first_material', TableRelation::T_ONE_TO_MANY, 'MaterialID'); |
||
131 | new TableRelation('field', 'structurefield', 'FieldID'); |
||
132 | new TableRelation('field', 'structure', 'structurefield.StructureID'); |
||
133 | new TableRelation('structurefield', 'field', 'FieldID'); |
||
134 | new TableRelation('structurefield', 'materialfield', 'FieldID'); |
||
135 | new TableRelation('structurefield', 'material', 'materialfield.MaterialID'); |
||
136 | new TableRelation('structure', 'structure_relation', 'StructureID', TableRelation::T_ONE_TO_MANY, 'parent_id', 'children_relations'); |
||
137 | new TableRelation('structure', 'structure', 'children_relations.child_id', TableRelation::T_ONE_TO_MANY, 'StructureID', 'children'); |
||
138 | new TableRelation('structure', 'structure_relation', 'StructureID', TableRelation::T_ONE_TO_MANY, 'child_id', 'parents_relations'); |
||
139 | new TableRelation('structure', 'structure', 'parents_relations.parent_id', TableRelation::T_ONE_TO_MANY, 'StructureID', 'parents'); |
||
140 | new TableRelation('structurematerial', 'structure_relation', 'StructureID', TableRelation::T_ONE_TO_MANY, 'parent_id'); |
||
141 | new TableRelation('groupright', 'right', 'RightID', TableRelation::T_ONE_TO_MANY); |
||
142 | |||
143 | m('activerecord')->relations(); |
||
144 | |||
145 | // Generate entities classes file |
||
146 | $generator = new Generator($this->database); |
||
147 | $file = md5($generator->entityHash()).'.php'; |
||
148 | if ($this->cache_refresh($file)) { |
||
149 | |||
150 | } |
||
151 | file_put_contents($file, '<?php '.$generator->createEntityClasses()); |
||
152 | |||
153 | // Include entities file |
||
154 | require($file); |
||
155 | |||
156 | return parent::prepare(); |
||
157 | } |
||
158 | |||
186 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.