Conditions | 12 |
Paths | 768 |
Total Lines | 60 |
Code Lines | 35 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 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 |
||
55 | public function findDemanded(Demand $demand) |
||
56 | { |
||
57 | $constraints = []; |
||
58 | $query = $this->createQuery(); |
||
59 | $query->setOrderings(['userName' => QueryInterface::ORDER_ASCENDING]); |
||
60 | // Username |
||
61 | if ($demand->getUserName() !== '') { |
||
62 | $searchConstraints = []; |
||
63 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('be_users'); |
||
64 | foreach (['userName', 'realName'] as $field) { |
||
65 | $searchConstraints[] = $query->like( |
||
66 | $field, |
||
67 | '%' . $queryBuilder->escapeLikeWildcards($demand->getUserName()) . '%' |
||
68 | ); |
||
69 | } |
||
70 | if (MathUtility::canBeInterpretedAsInteger($demand->getUserName())) { |
||
71 | $searchConstraints[] = $query->equals('uid', (int)$demand->getUserName()); |
||
72 | } |
||
73 | $constraints[] = $query->logicalOr($searchConstraints); |
||
74 | } |
||
75 | // Only display admin users |
||
76 | if ($demand->getUserType() == Demand::USERTYPE_ADMINONLY) { |
||
77 | $constraints[] = $query->equals('admin', 1); |
||
78 | } |
||
79 | // Only display non-admin users |
||
80 | if ($demand->getUserType() == Demand::USERTYPE_USERONLY) { |
||
81 | $constraints[] = $query->equals('admin', 0); |
||
82 | } |
||
83 | // Only display active users |
||
84 | if ($demand->getStatus() == Demand::STATUS_ACTIVE) { |
||
85 | $constraints[] = $query->equals('disable', 0); |
||
86 | } |
||
87 | // Only display in-active users |
||
88 | if ($demand->getStatus() == Demand::STATUS_INACTIVE) { |
||
89 | $constraints[] = $query->logicalOr($query->equals('disable', 1)); |
||
90 | } |
||
91 | // Not logged in before |
||
92 | if ($demand->getLogins() == Demand::LOGIN_NONE) { |
||
93 | $constraints[] = $query->equals('lastlogin', 0); |
||
94 | } |
||
95 | // At least one login |
||
96 | if ($demand->getLogins() == Demand::LOGIN_SOME) { |
||
97 | $constraints[] = $query->logicalNot($query->equals('lastlogin', 0)); |
||
98 | } |
||
99 | // In backend user group |
||
100 | // @TODO: Refactor for real n:m relations |
||
101 | if ($demand->getBackendUserGroup()) { |
||
102 | $constraints[] = $query->logicalOr([ |
||
103 | $query->equals('usergroup', (int)$demand->getBackendUserGroup()), |
||
104 | $query->like('usergroup', (int)$demand->getBackendUserGroup() . ',%'), |
||
105 | $query->like('usergroup', '%,' . (int)$demand->getBackendUserGroup()), |
||
106 | $query->like('usergroup', '%,' . (int)$demand->getBackendUserGroup() . ',%') |
||
107 | ]); |
||
108 | } |
||
109 | if ($constraints !== []) { |
||
110 | $query->matching($query->logicalAnd($constraints)); |
||
111 | } |
||
112 | /** @var QueryResult $result */ |
||
113 | $result = $query->execute(); |
||
114 | return $result; |
||
115 | } |
||
158 |
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: