Conditions | 10 |
Paths | 80 |
Total Lines | 57 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
Changes | 8 | ||
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 |
||
60 | protected function matchValue($requestPath) |
||
61 | { |
||
62 | /** @var Uri $uri */ |
||
63 | $uri = $this->bootstrap->getActiveRequestHandler()->getHttpRequest()->getUri(); |
||
|
|||
64 | $relativeUrl = rtrim($uri->getPath(), '/'); |
||
65 | if($this->startsWith($relativeUrl, '/')){ |
||
66 | $tempRelativeUrl = substr($relativeUrl, 1); |
||
67 | } |
||
68 | $relativeUrlWithQueryString = $relativeUrl . ($uri->getQuery() ? '?' . $uri->getQuery() : ''); |
||
69 | $absoluteUrl = $uri->getHost() . $relativeUrl; |
||
70 | $absoluteUrlWithQueryString = $uri->getHost() . $relativeUrlWithQueryString; |
||
71 | |||
72 | if (empty($relativeUrl)) { |
||
73 | return false; |
||
74 | } |
||
75 | |||
76 | /** @var QueryBuilder $queryBuilder */ |
||
77 | $queryBuilder = $this->entityManager->createQueryBuilder(); |
||
78 | $queryBuilder->select('n') |
||
79 | ->distinct() |
||
80 | ->from('TYPO3\TYPO3CR\Domain\Model\NodeData', 'n') |
||
81 | ->where('n.workspace = :workspace') |
||
82 | ->setParameter('workspace', 'live') |
||
83 | ->andWhere('n.properties LIKE :relativeUrl') |
||
84 | ->setParameter('relativeUrl', '%"redirectUrl"%' . str_replace('/', '\\\\\\/', $tempRelativeUrl) . '%'); |
||
85 | $query = $queryBuilder->getQuery(); |
||
86 | $nodes = $query->getResult(); |
||
87 | if (empty($nodes)) { |
||
88 | return false; |
||
89 | } |
||
90 | |||
91 | foreach ($nodes as $node) { |
||
92 | /** @var NodeData $node */ |
||
93 | // Prevent partial matches |
||
94 | $redirectUrl = preg_replace('#^https?://#', '', $node->getProperty('redirectUrl')); |
||
95 | if($this->endsWith($redirectUrl, '/') === TRUE){ |
||
96 | $redirectUrl = substr($redirectUrl, 0, -1); |
||
97 | } |
||
98 | if($this->startsWith($relativeUrl, '/') === TRUE){ |
||
99 | $relativeUrl = substr($relativeUrl, 1); |
||
100 | } |
||
101 | |||
102 | if (in_array($redirectUrl, |
||
103 | array($relativeUrl, $relativeUrlWithQueryString, $absoluteUrl, $absoluteUrlWithQueryString), true)) { |
||
104 | |||
105 | $matchingNode = $node; |
||
106 | break; |
||
107 | } |
||
108 | } |
||
109 | if (!isset($matchingNode)) { |
||
110 | return false; |
||
111 | } |
||
112 | |||
113 | $this->setName('node'); |
||
114 | $this->value = $matchingNode->getPath(); |
||
115 | return true; |
||
116 | } |
||
117 | |||
128 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: