These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||
5 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||
6 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||
7 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||
8 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||
9 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||
10 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
11 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
12 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
13 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||
14 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
15 | * |
||
16 | * This software consists of voluntary contributions made by many individuals |
||
17 | * and is licensed under the MIT license. For more information, see |
||
18 | * <http://www.doctrine-project.org>. |
||
19 | */ |
||
20 | |||
21 | namespace Doctrine\ORM\Cache\Persister\Entity; |
||
22 | |||
23 | use Doctrine\ORM\Persisters\Entity\EntityPersister; |
||
24 | use Doctrine\ORM\Mapping\ClassMetadata; |
||
25 | use Doctrine\ORM\EntityManagerInterface; |
||
26 | use Doctrine\ORM\Cache\ConcurrentRegion; |
||
27 | use Doctrine\ORM\Cache\EntityCacheKey; |
||
28 | |||
29 | /** |
||
30 | * Specific read-write entity persister |
||
31 | * |
||
32 | * @author Fabio B. Silva <[email protected]> |
||
33 | * @author Guilherme Blanco <[email protected]> |
||
34 | * @since 2.5 |
||
35 | */ |
||
36 | class ReadWriteCachedEntityPersister extends AbstractEntityPersister |
||
37 | { |
||
38 | /** |
||
39 | * @param \Doctrine\ORM\Persisters\Entity\EntityPersister $persister The entity persister to cache. |
||
40 | * @param \Doctrine\ORM\Cache\ConcurrentRegion $region The entity cache region. |
||
41 | * @param \Doctrine\ORM\EntityManagerInterface $em The entity manager. |
||
42 | * @param \Doctrine\ORM\Mapping\ClassMetadata $class The entity metadata. |
||
43 | */ |
||
44 | 33 | public function __construct(EntityPersister $persister, ConcurrentRegion $region, EntityManagerInterface $em, ClassMetadata $class) |
|
45 | { |
||
46 | 33 | parent::__construct($persister, $region, $em, $class); |
|
47 | 33 | } |
|
48 | |||
49 | /** |
||
50 | * {@inheritdoc} |
||
51 | */ |
||
52 | 1 | public function afterTransactionComplete() |
|
53 | { |
||
54 | 1 | $isChanged = true; |
|
55 | |||
56 | 1 | if (isset($this->queuedCache['update'])) { |
|
57 | 1 | foreach ($this->queuedCache['update'] as $item) { |
|
58 | 1 | $this->region->evict($item['key']); |
|
59 | |||
60 | 1 | $isChanged = true; |
|
61 | } |
||
62 | } |
||
63 | |||
64 | 1 | if (isset($this->queuedCache['delete'])) { |
|
65 | 1 | foreach ($this->queuedCache['delete'] as $item) { |
|
66 | 1 | $this->region->evict($item['key']); |
|
67 | |||
68 | 1 | $isChanged = true; |
|
69 | } |
||
70 | } |
||
71 | |||
72 | 1 | if ($isChanged) { |
|
73 | 1 | $this->timestampRegion->update($this->timestampKey); |
|
74 | } |
||
75 | |||
76 | 1 | $this->queuedCache = []; |
|
77 | 1 | } |
|
78 | |||
79 | /** |
||
80 | * {@inheritdoc} |
||
81 | */ |
||
82 | 3 | public function afterTransactionRolledBack() |
|
83 | { |
||
84 | 3 | if (isset($this->queuedCache['update'])) { |
|
85 | 2 | foreach ($this->queuedCache['update'] as $item) { |
|
86 | 2 | $this->region->evict($item['key']); |
|
87 | } |
||
88 | } |
||
89 | |||
90 | 3 | if (isset($this->queuedCache['delete'])) { |
|
91 | 2 | foreach ($this->queuedCache['delete'] as $item) { |
|
92 | 2 | $this->region->evict($item['key']); |
|
93 | } |
||
94 | } |
||
95 | |||
96 | 3 | $this->queuedCache = []; |
|
97 | 3 | } |
|
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | 6 | public function delete($entity) |
|
103 | { |
||
104 | 6 | $key = new EntityCacheKey($this->class->rootEntityName, $this->uow->getEntityIdentifier($entity)); |
|
105 | 6 | $lock = $this->region->lock($key); |
|
0 ignored issues
–
show
|
|||
106 | 6 | $deleted = $this->persister->delete($entity); |
|
107 | |||
108 | 6 | if ($deleted) { |
|
109 | $this->region->evict($key); |
||
110 | } |
||
111 | |||
112 | 6 | if ($lock === null) { |
|
113 | 2 | return $deleted; |
|
114 | } |
||
115 | |||
116 | 4 | $this->queuedCache['delete'][] = [ |
|
117 | 4 | 'lock' => $lock, |
|
118 | 4 | 'key' => $key |
|
119 | ]; |
||
120 | |||
121 | 4 | return $deleted; |
|
122 | } |
||
123 | |||
124 | /** |
||
125 | * {@inheritdoc} |
||
126 | */ |
||
127 | 6 | public function update($entity) |
|
128 | { |
||
129 | 6 | $key = new EntityCacheKey($this->class->rootEntityName, $this->uow->getEntityIdentifier($entity)); |
|
130 | 6 | $lock = $this->region->lock($key); |
|
0 ignored issues
–
show
It seems like you code against a concrete implementation and not the interface
Doctrine\ORM\Cache\Region as the method lock() does only exist in the following implementations of said interface: Doctrine\ORM\Cache\Region\FileLockRegion , Doctrine\Tests\Mocks\ConcurrentRegionMock .
Let’s take a look at an example: interface User
{
/** @return string */
public function getPassword();
}
class MyUser implements User
{
public function getPassword()
{
// return something
}
public function getDisplayName()
{
// return some name.
}
}
class AuthSystem
{
public function authenticate(User $user)
{
$this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
// do something.
}
}
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
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types
inside the if block in such a case.
Loading history...
|
|||
131 | |||
132 | 6 | $this->persister->update($entity); |
|
133 | |||
134 | 6 | if ($lock === null) { |
|
135 | 2 | return; |
|
136 | } |
||
137 | |||
138 | 4 | $this->queuedCache['update'][] = [ |
|
139 | 4 | 'lock' => $lock, |
|
140 | 4 | 'key' => $key |
|
141 | ]; |
||
142 | 4 | } |
|
143 | } |
||
144 |
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: