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