Conditions | 12 |
Paths | 288 |
Total Lines | 60 |
Code Lines | 33 |
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 |
||
51 | public function save() |
||
52 | { |
||
53 | // check all changed fields |
||
54 | $modelName = get_class($this); |
||
55 | $shadowModel = new $modelName(); |
||
56 | /* @var $shadowModel Ajde_Model */ |
||
57 | $shadowModel->loadByPK($this->getPK()); |
||
58 | if ($shadowModel->_hasMeta) { |
||
59 | $shadowModel->populateMeta(); |
||
60 | } |
||
61 | |||
62 | // old values |
||
63 | $oldValues = $shadowModel->values(); |
||
64 | foreach ($oldValues as &$oldValue) { |
||
65 | @$oldValue = (string) $oldValue; |
||
66 | } |
||
67 | |||
68 | // populate meta of current model, but don't override |
||
69 | if ($this->_hasMeta) { |
||
70 | $this->populateMeta(false, false); |
||
71 | } |
||
72 | |||
73 | // new values |
||
74 | $newValues = $this->values(); |
||
75 | foreach ($newValues as $k => &$newValue) { |
||
76 | if ($k == 'meta_4') { |
||
77 | // die('hier'); |
||
78 | } |
||
79 | @$newValue = (string) $newValue; |
||
80 | } |
||
81 | |||
82 | // ignore fields |
||
83 | foreach ($this->_ignoreFieldInRevision as $ignoreField) { |
||
84 | unset($oldValues[$ignoreField]); |
||
85 | unset($newValues[$ignoreField]); |
||
86 | } |
||
87 | |||
88 | // ignore fields |
||
89 | foreach ($this->_ignoreFieldInRevisionIfEmpty as $ignoreField) { |
||
90 | if (!isset($newValues[$ignoreField]) || empty($newValues[$ignoreField])) { |
||
91 | unset($oldValues[$ignoreField]); |
||
92 | unset($newValues[$ignoreField]); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | if ($diffs = array_diff_assoc($oldValues, $newValues)) { |
||
97 | foreach ($diffs as $diffField => $diffValue) { |
||
98 | $revision = new RevisionModel(); |
||
99 | $revision->model = $this->getModelName(); |
||
100 | $revision->foreignkey = $this->getPK(); |
||
101 | $revision->user = UserModel::getLoggedIn(); |
||
102 | $revision->field = $diffField; |
||
103 | $revision->old = issetor($oldValues[$diffField]); |
||
104 | $revision->new = issetor($newValues[$diffField]); |
||
105 | $revision->insert(); |
||
106 | } |
||
107 | } |
||
108 | |||
109 | return parent::save(); |
||
110 | } |
||
111 | } |
||
112 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: