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 |
||
81 | public function createSearchQuery(RaSecondFactorQuery $query) |
||
82 | { |
||
83 | $queryBuilder = $this |
||
84 | ->createQueryBuilder('sf'); |
||
85 | |||
86 | // Modify query to filter on authorization |
||
87 | $this->authorizationRepositoryFilter->filter($queryBuilder, $query->authorizationContext, 'sf.id', 'sf.institution', 'iac'); |
||
88 | |||
89 | if ($query->name) { |
||
90 | $queryBuilder->andWhere('sf.name LIKE :name')->setParameter('name', sprintf('%%%s%%', $query->name)); |
||
91 | } |
||
92 | |||
93 | if ($query->type) { |
||
94 | $queryBuilder->andWhere('sf.type = :type')->setParameter('type', $query->type); |
||
95 | } |
||
96 | |||
97 | if ($query->secondFactorId) { |
||
98 | $queryBuilder |
||
99 | ->andWhere('sf.secondFactorId = :secondFactorId') |
||
100 | ->setParameter('secondFactorId', $query->secondFactorId); |
||
101 | } |
||
102 | |||
103 | if ($query->email) { |
||
104 | $queryBuilder->andWhere('sf.email LIKE :email')->setParameter('email', sprintf('%%%s%%', $query->email)); |
||
105 | } |
||
106 | |||
107 | if ($query->institution) { |
||
108 | $queryBuilder->andWhere('sf.institution = :institution')->setParameter('institution', $query->institution); |
||
109 | } |
||
110 | |||
111 | if ($query->status) { |
||
112 | $stringStatus = $query->status; |
||
113 | if (!SecondFactorStatus::isValidStatus($stringStatus)) { |
||
114 | throw new RuntimeException(sprintf( |
||
115 | 'Received invalid status "%s" in RaSecondFactorRepository::createSearchQuery', |
||
116 | is_object($stringStatus) ? get_class($stringStatus) : (string) $stringStatus |
||
117 | )); |
||
118 | } |
||
119 | |||
120 | // we need to resolve the string value to database value using the correct doctrine type. Normally this is |
||
121 | // done by doctrine itself, however the queries PagerFanta creates somehow manages to mangle this... |
||
122 | // so we do it by hand |
||
123 | $doctrineType = Type::getType(SecondFactorStatusType::NAME); |
||
124 | $secondFactorStatus = SecondFactorStatus::$stringStatus(); |
||
125 | |||
126 | $databaseValue = $doctrineType->convertToDatabaseValue( |
||
127 | $secondFactorStatus, |
||
128 | $this->getEntityManager()->getConnection()->getDatabasePlatform() |
||
129 | ); |
||
130 | |||
131 | $queryBuilder->andWhere('sf.status = :status')->setParameter('status', $databaseValue); |
||
132 | } |
||
133 | |||
134 | switch ($query->orderBy) { |
||
135 | case 'name': |
||
136 | case 'type': |
||
137 | case 'secondFactorId': |
||
138 | case 'email': |
||
139 | case 'institution': |
||
140 | case 'status': |
||
141 | $queryBuilder->orderBy( |
||
142 | sprintf('sf.%s', $query->orderBy), |
||
143 | $query->orderDirection === 'desc' ? 'DESC' : 'ASC' |
||
144 | ); |
||
145 | break; |
||
146 | } |
||
147 | |||
148 | return $queryBuilder->getQuery(); |
||
149 | } |
||
150 | |||
185 |
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.