Complex classes like TbEditableField often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TbEditableField, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class TbEditableField extends TbEditable |
||
19 | { |
||
20 | /** |
||
21 | * @var CActiveRecord ActiveRecord to be updated. |
||
22 | */ |
||
23 | public $model = null; |
||
24 | /** |
||
25 | * @var string attribute name. |
||
26 | */ |
||
27 | public $attribute = null; |
||
28 | |||
29 | /** |
||
30 | * @var mixed instance of model that is created always: |
||
31 | * E.g. if related model does not exist, it will be `newed` to be able to get Attribute label, etc |
||
32 | * for live update. |
||
33 | */ |
||
34 | private $staticModel = null; |
||
35 | |||
36 | /** |
||
37 | * initialization of widget |
||
38 | * |
||
39 | */ |
||
40 | public function init() |
||
41 | { |
||
42 | if (!$this->model) { |
||
43 | throw new CException('Parameter "model" should be provided for TbEditableField'); |
||
44 | } |
||
45 | |||
46 | if (!$this->attribute) { |
||
47 | throw new CException('Parameter "attribute" should be provided for TbEditableField'); |
||
48 | } |
||
49 | |||
50 | $originalModel = $this->model; |
||
51 | $originalAttribute = $this->attribute; |
||
52 | $originalText = strlen($this->text) ? $this->text : CHtml::value($this->model, $this->attribute); |
||
53 | |||
54 | //if apply set manually to false --> just render text, no js plugin applied |
||
55 | if($this->apply === false) { |
||
56 | $this->text = $originalText; |
||
57 | } else { |
||
58 | $this->apply = true; |
||
59 | } |
||
60 | |||
61 | //try to resolve related model (if attribute contains '.') |
||
62 | $resolved = $this->resolveModels($this->model, $this->attribute); |
||
63 | $this->model = $resolved['model']; |
||
64 | $this->attribute = $resolved['attribute']; |
||
65 | $this->staticModel = $resolved['staticModel']; |
||
66 | $staticModel = $this->staticModel; |
||
67 | $isMongo = $resolved['isMongo']; |
||
68 | $isFormModel = $this->model instanceOf CFormModel; |
||
|
|||
69 | |||
70 | //if real (related) model not exists --> just print text |
||
71 | if(!$this->model) { |
||
72 | $this->apply = false; |
||
73 | $this->text = $originalText; |
||
74 | } |
||
75 | |||
76 | |||
77 | //for security reason only safe attributes can be editable (e.g. defined in rules of model) |
||
78 | //just print text (see 'run' method) |
||
79 | if (!$staticModel->isAttributeSafe($this->attribute)) { |
||
80 | $this->apply = false; |
||
81 | $this->text = $originalText; |
||
82 | } |
||
83 | |||
84 | /* |
||
85 | try to detect type from metadata if not set |
||
86 | */ |
||
87 | if ($this->type === null) { |
||
88 | $this->type = 'text'; |
||
89 | if (!$isMongo && !$isFormModel && array_key_exists($this->attribute, $staticModel->tableSchema->columns)) { |
||
90 | $dbType = $staticModel->tableSchema->columns[$this->attribute]->dbType; |
||
91 | if($dbType == 'date') { |
||
92 | $this->type = 'date'; |
||
93 | } |
||
94 | if($dbType == 'datetime') { |
||
95 | $this->type = 'datetime'; |
||
96 | } |
||
97 | if(stripos($dbType, 'text') !== false) { |
||
98 | $this->type = 'textarea'; |
||
99 | } |
||
100 | } |
||
101 | } |
||
102 | |||
103 | //name |
||
104 | if(empty($this->name)) { |
||
105 | $this->name = $isMongo ? $originalAttribute : $this->attribute; |
||
106 | } |
||
107 | |||
108 | //pk (for mongo takes pk from parent!) |
||
109 | $pkModel = $isMongo ? $originalModel : $this->model; |
||
110 | if(!$isFormModel) { |
||
111 | if($pkModel && !$pkModel->isNewRecord) { |
||
112 | $this->pk = $pkModel->primaryKey; |
||
113 | } |
||
114 | } else { |
||
115 | //formModel does not have pk, so set `send` option to `always` (send without pk) |
||
116 | if(empty($this->send) && empty($this->options['send'])) { |
||
117 | $this->send = 'always'; |
||
118 | } |
||
119 | } |
||
120 | |||
121 | parent::init(); |
||
122 | |||
123 | /* |
||
124 | If text not defined, generate it from model attribute for types except lists ('select', 'checklist' etc) |
||
125 | For lists keep it empty to apply autotext. |
||
126 | $this->_prepareToAutotext calculated in parent class TbEditable.php |
||
127 | */ |
||
128 | if (!strlen($this->text) && !$this->_prepareToAutotext) { |
||
129 | $this->text = $originalText; |
||
130 | } |
||
131 | |||
132 | //set value directly for autotext generation |
||
133 | if($this->model && $this->_prepareToAutotext) { |
||
134 | $this->value = CHtml::value($this->model, $this->attribute); |
||
135 | } |
||
136 | |||
137 | //generate title from attribute label |
||
138 | if ($this->title === null) { |
||
139 | $titles = array( |
||
140 | 'Select' => array('select', 'date'), |
||
141 | 'Check' => array('checklist') |
||
142 | ); |
||
143 | $title = Yii::t('TbEditableField.editable', 'Enter'); |
||
144 | foreach($titles as $t => $types) { |
||
145 | if(in_array($this->type, $types)) { |
||
146 | $title = Yii::t('TbEditableField.editable', $t); |
||
147 | } |
||
148 | } |
||
149 | $this->title = $title . ' ' . $staticModel->getAttributeLabel($this->attribute); |
||
150 | } else { |
||
151 | $this->title = strtr($this->title, array('{label}' => $staticModel->getAttributeLabel($this->attribute))); |
||
152 | } |
||
153 | |||
154 | //scenario |
||
155 | if ($pkModel) { |
||
156 | if ((is_array($this->params) && !isset($this->params['scenario'])) || $this->params === null) { |
||
157 | $this->params['scenario'] = $pkModel->getScenario(); |
||
158 | } elseif (strlen($this->params)) { |
||
159 | $orig = $this->params; |
||
160 | if (strpos($orig, 'js:') === 0) { |
||
161 | $orig = substr($orig, 3); |
||
162 | } |
||
163 | $orig = "params = ($orig).call(this, params);\n"; |
||
164 | $this->params = "js: function(params) { |
||
165 | params.scenario = '" . $pkModel->getScenario() . "'; |
||
166 | $orig |
||
167 | return params; |
||
168 | }"; |
||
169 | } |
||
170 | |||
171 | } |
||
172 | } |
||
173 | |||
174 | public function getSelector() |
||
178 | |||
179 | |||
180 | /** |
||
181 | * Checks is model is instance of mongo model |
||
182 | * see: http://www.yiiframework.com/extension/yiimongodbsuite |
||
183 | * |
||
184 | * @param mixed $model |
||
185 | * @return bool |
||
186 | */ |
||
187 | public static function isMongo($model) |
||
191 | |||
192 | /** |
||
193 | * Resolves model and returns array of values: |
||
194 | * - staticModel: static class of model, need for checki safety of attribute |
||
195 | * - real model: containing attribute. Can be null |
||
196 | * - attribute: it will be without dots for activerecords |
||
197 | * |
||
198 | * @param CActiveRecord $model |
||
199 | * @param string $attribute |
||
200 | * |
||
201 | * @throws CException |
||
202 | * @return array |
||
203 | */ |
||
204 | public static function resolveModels($model, $attribute) |
||
256 | } |
||
257 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.