Conditions | 10 |
Paths | 8 |
Total Lines | 34 |
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 |
||
66 | public function getTenant(): ?TenantInterface |
||
67 | { |
||
68 | $currentRequest = $this->requestStack->getCurrentRequest(); |
||
69 | if ($currentRequest && $this->requestStack->getCurrentRequest()->attributes->get('exception') instanceof TenantNotFoundException) { |
||
70 | return null; |
||
71 | } |
||
72 | |||
73 | if (null === $this->tenant) { |
||
74 | if (null !== $currentRequest) { |
||
75 | $cacheKey = self::getCacheKey($currentRequest->getHost()); |
||
76 | |||
77 | if ($this->cacheProvider->contains($cacheKey) && ($tenant = $this->cacheProvider->fetch($cacheKey)) instanceof TenantInterface) { |
||
78 | // solution for serialization |
||
79 | if (null !== $tenant->getHomepage()) { |
||
80 | $tenant->setHomepage($this->entityManager->find(Route::class, $tenant->getHomepage()->getId())); |
||
81 | } |
||
82 | if (null !== $tenant->getOutputChannel()) { |
||
83 | $tenant->setOutputChannel($this->entityManager->find(OutputChannel::class, $tenant->getOutputChannel()->getId())); |
||
84 | } |
||
85 | parent::setTenant($this->attachToEntityManager($tenant)); |
||
86 | } else { |
||
87 | $tenant = $this->tenantResolver->resolve( |
||
88 | $currentRequest ? $currentRequest->getHost() : null |
||
89 | ); |
||
90 | |||
91 | parent::setTenant($tenant); |
||
92 | |||
93 | $this->cacheProvider->save($cacheKey, $tenant); |
||
94 | } |
||
95 | } |
||
96 | } |
||
97 | |||
98 | return $this->tenant; |
||
99 | } |
||
100 | |||
148 |
The
EntityManager
might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.