Conditions | 13 |
Paths | 78 |
Total Lines | 41 |
Code Lines | 25 |
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 |
||
74 | protected function validateKeys() |
||
75 | { |
||
76 | $fieldNames = array_map(function ($field) { |
||
77 | return $field->name; |
||
78 | }, $this->descriptor->fields); |
||
79 | if (isset($this->descriptor->primaryKey)) { |
||
80 | $primaryKey = is_array($this->descriptor->primaryKey) ? $this->descriptor->primaryKey : [$this->descriptor->primaryKey]; |
||
81 | foreach ($primaryKey as $primaryKeyField) { |
||
82 | if (!in_array($primaryKeyField, $fieldNames)) { |
||
83 | $this->addError( |
||
84 | SchemaValidationError::SCHEMA_VIOLATION, |
||
85 | "primary key must refer to a field name ({$primaryKeyField})" |
||
86 | ); |
||
87 | } |
||
88 | } |
||
89 | } |
||
90 | if (isset($this->descriptor->foreignKeys)) { |
||
91 | foreach ($this->descriptor->foreignKeys as $foreignKey) { |
||
92 | $fields = is_array($foreignKey->fields) ? $foreignKey->fields : [$foreignKey->fields]; |
||
93 | foreach ($fields as $field) { |
||
94 | if (!in_array($field, $fieldNames)) { |
||
95 | $this->addError( |
||
96 | SchemaValidationError::SCHEMA_VIOLATION, |
||
97 | "foreign key fields must refer to a field name ({$field})" |
||
98 | ); |
||
99 | } |
||
100 | } |
||
101 | if ($foreignKey->reference->resource == '') { |
||
102 | // empty resource = reference to self |
||
103 | foreach ($foreignKey->reference->fields as $field) { |
||
104 | if (!in_array($field, $fieldNames)) { |
||
105 | $this->addError( |
||
106 | SchemaValidationError::SCHEMA_VIOLATION, |
||
107 | "foreign key reference to self must refer to a field name ({$field})" |
||
108 | ); |
||
109 | } |
||
110 | } |
||
111 | } |
||
112 | } |
||
113 | } |
||
114 | } |
||
115 | } |
||
116 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: