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