Conditions | 16 |
Paths | 480 |
Total Lines | 69 |
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 |
||
91 | public function createSearchQuery(RaSecondFactorQuery $query) |
||
92 | { |
||
93 | $queryBuilder = $this |
||
94 | ->createQueryBuilder('sf'); |
||
95 | |||
96 | // Modify query to filter on authorization |
||
97 | $this->authorizationRepositoryFilter->filter($queryBuilder, $query->authorizationContext, 'sf.id', 'sf.institution', 'iac'); |
||
98 | |||
99 | if ($query->name) { |
||
100 | $queryBuilder->andWhere('sf.name LIKE :name')->setParameter('name', sprintf('%%%s%%', $query->name)); |
||
101 | } |
||
102 | |||
103 | if ($query->type) { |
||
104 | $queryBuilder->andWhere('sf.type = :type')->setParameter('type', $query->type); |
||
105 | } |
||
106 | |||
107 | if ($query->secondFactorId) { |
||
108 | $queryBuilder |
||
109 | ->andWhere('sf.secondFactorId = :secondFactorId') |
||
110 | ->setParameter('secondFactorId', $query->secondFactorId); |
||
111 | } |
||
112 | |||
113 | if ($query->email) { |
||
114 | $queryBuilder->andWhere('sf.email LIKE :email')->setParameter('email', sprintf('%%%s%%', $query->email)); |
||
115 | } |
||
116 | |||
117 | if ($query->institution) { |
||
118 | $queryBuilder->andWhere('sf.institution = :institution')->setParameter('institution', $query->institution); |
||
119 | } |
||
120 | |||
121 | if ($query->status) { |
||
122 | $stringStatus = $query->status; |
||
123 | if (!SecondFactorStatus::isValidStatus($stringStatus)) { |
||
124 | throw new RuntimeException(sprintf( |
||
125 | 'Received invalid status "%s" in RaSecondFactorRepository::createSearchQuery', |
||
126 | is_object($stringStatus) ? get_class($stringStatus) : (string) $stringStatus |
||
127 | )); |
||
128 | } |
||
129 | |||
130 | // we need to resolve the string value to database value using the correct doctrine type. Normally this is |
||
131 | // done by doctrine itself, however the queries PagerFanta creates somehow manages to mangle this... |
||
132 | // so we do it by hand |
||
133 | $doctrineType = Type::getType(SecondFactorStatusType::NAME); |
||
134 | $secondFactorStatus = SecondFactorStatus::$stringStatus(); |
||
135 | |||
136 | $databaseValue = $doctrineType->convertToDatabaseValue( |
||
137 | $secondFactorStatus, |
||
138 | $this->getEntityManager()->getConnection()->getDatabasePlatform() |
||
139 | ); |
||
140 | |||
141 | $queryBuilder->andWhere('sf.status = :status')->setParameter('status', $databaseValue); |
||
142 | } |
||
143 | |||
144 | switch ($query->orderBy) { |
||
145 | case 'name': |
||
146 | case 'type': |
||
147 | case 'secondFactorId': |
||
148 | case 'email': |
||
149 | case 'institution': |
||
150 | case 'status': |
||
151 | $queryBuilder->orderBy( |
||
152 | sprintf('sf.%s', $query->orderBy), |
||
153 | $query->orderDirection === 'desc' ? 'DESC' : 'ASC' |
||
154 | ); |
||
155 | break; |
||
156 | } |
||
157 | |||
158 | return $queryBuilder->getQuery(); |
||
159 | } |
||
160 | |||
195 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.