| Conditions | 5 |
| Paths | 6 |
| Total Lines | 57 |
| Code Lines | 36 |
| 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 |
||
| 22 | public function search(array $params) |
||
| 23 | { |
||
| 24 | $query = Account::find() |
||
| 25 | ->leftJoin( |
||
| 26 | AccountStats::tableName(), |
||
| 27 | 'account.id=account_stats.account_id and account_stats.id = (SELECT MAX(id) FROM account_stats WHERE account_stats.account_id=account.id)' |
||
| 28 | ); |
||
| 29 | |||
| 30 | $tags = ArrayHelper::getValue($params, 'filter.tags'); |
||
| 31 | unset($params['filter']['tags']); |
||
| 32 | |||
| 33 | if ($tags) { |
||
| 34 | $tags = StringHelper::explode($tags, ',', true, true); |
||
| 35 | $tag = array_shift($tags); |
||
| 36 | $accountIds = $this->getTaggedAccountIds($tag); |
||
| 37 | |||
| 38 | foreach ($tags as $tag) { |
||
| 39 | $accountIds = array_intersect($accountIds, $this->getTaggedAccountIds($tag)); |
||
| 40 | } |
||
| 41 | |||
| 42 | $query->andWhere(['account.id' => $accountIds]); |
||
| 43 | } |
||
| 44 | |||
| 45 | $dataFilter = \Yii::createObject([ |
||
| 46 | 'class' => ActiveDataFilter::class, |
||
| 47 | 'searchModel' => DataFilterForm::class, |
||
| 48 | ]); |
||
| 49 | if ($dataFilter->load($params)) { |
||
| 50 | $filter = $dataFilter->build(); |
||
| 51 | if ($filter === false) { |
||
| 52 | return $dataFilter; |
||
| 53 | } |
||
| 54 | $query->andWhere($filter); |
||
| 55 | } |
||
| 56 | |||
| 57 | $dataProvider = new ActiveDataProvider([ |
||
| 58 | 'query' => $query, |
||
| 59 | ]); |
||
| 60 | |||
| 61 | $dataProvider->sort->attributes['followed_by'] = [ |
||
| 62 | 'asc' => ['account_stats.followed_by' => SORT_ASC], |
||
| 63 | 'desc' => ['account_stats.followed_by' => SORT_DESC], |
||
| 64 | ]; |
||
| 65 | $dataProvider->sort->attributes['follows'] = [ |
||
| 66 | 'asc' => ['account_stats.follows' => SORT_ASC], |
||
| 67 | 'desc' => ['account_stats.follows' => SORT_DESC], |
||
| 68 | ]; |
||
| 69 | $dataProvider->sort->attributes['media'] = [ |
||
| 70 | 'asc' => ['account_stats.media' => SORT_ASC], |
||
| 71 | 'desc' => ['account_stats.media' => SORT_DESC], |
||
| 72 | ]; |
||
| 73 | $dataProvider->sort->attributes['er'] = [ |
||
| 74 | 'asc' => ['account_stats.er' => SORT_ASC], |
||
| 75 | 'desc' => ['account_stats.er' => SORT_DESC], |
||
| 76 | ]; |
||
| 77 | |||
| 78 | return $dataProvider; |
||
| 79 | } |
||
| 95 | } |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: