Conditions | 4 |
Paths | 6 |
Total Lines | 52 |
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 |
||
20 | public function search(array $params) |
||
21 | { |
||
22 | $query = Account::find() |
||
23 | ->leftJoin( |
||
24 | AccountStats::tableName(), |
||
25 | 'account.id=account_stats.account_id and account_stats.id = (SELECT MAX(id) FROM account_stats WHERE account_stats.account_id=account.id)' |
||
26 | ); |
||
27 | |||
28 | $tags = ArrayHelper::getValue($params, 'filter.tags'); |
||
29 | unset($params['filter']['tags']); |
||
30 | |||
31 | if ($tags) { |
||
32 | $manager = \Yii::createObject(AccountManager::class); |
||
33 | $accountIds = $manager->findByTags($tags, \Yii::$app->user->id); |
||
34 | |||
35 | $query->andWhere(['account.id' => $accountIds]); |
||
36 | } |
||
37 | |||
38 | $dataFilter = \Yii::createObject([ |
||
39 | 'class' => ActiveDataFilter::class, |
||
40 | 'searchModel' => DataFilterForm::class, |
||
41 | ]); |
||
42 | if ($dataFilter->load($params)) { |
||
43 | $filter = $dataFilter->build(); |
||
44 | if ($filter === false) { |
||
45 | return $dataFilter; |
||
46 | } |
||
47 | $query->andWhere($filter); |
||
48 | } |
||
49 | |||
50 | $dataProvider = new ActiveDataProvider([ |
||
51 | 'query' => $query, |
||
52 | ]); |
||
53 | |||
54 | $dataProvider->sort->attributes['followed_by'] = [ |
||
55 | 'asc' => ['account_stats.followed_by' => SORT_ASC], |
||
56 | 'desc' => ['account_stats.followed_by' => SORT_DESC], |
||
57 | ]; |
||
58 | $dataProvider->sort->attributes['follows'] = [ |
||
59 | 'asc' => ['account_stats.follows' => SORT_ASC], |
||
60 | 'desc' => ['account_stats.follows' => SORT_DESC], |
||
61 | ]; |
||
62 | $dataProvider->sort->attributes['media'] = [ |
||
63 | 'asc' => ['account_stats.media' => SORT_ASC], |
||
64 | 'desc' => ['account_stats.media' => SORT_DESC], |
||
65 | ]; |
||
66 | $dataProvider->sort->attributes['er'] = [ |
||
67 | 'asc' => ['account_stats.er' => SORT_ASC], |
||
68 | 'desc' => ['account_stats.er' => SORT_DESC], |
||
69 | ]; |
||
70 | |||
71 | return $dataProvider; |
||
72 | } |
||
73 | } |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: