Failed Conditions
Push — master ( 6744b4...2b8acb )
by Marco
60:45 queued 60:36
created

Entity/NonStrictReadWriteCachedEntityPersister.php (4 issues)

Upgrade to new PHP Analysis Engine

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\Cache\EntityCacheKey;
24
25
/**
26
 * Specific non-strict read/write cached entity persister
27
 *
28
 * @author Fabio B. Silva <[email protected]>
29
 * @author Guilherme Blanco <[email protected]>
30
 * @since  2.5
31
 */
32
class NonStrictReadWriteCachedEntityPersister extends AbstractEntityPersister
33
{
34
    /**
35
     * {@inheritdoc}
36
     */
37 96
    public function afterTransactionComplete()
38
    {
39 96
        $isChanged = false;
40
41 96 View Code Duplication
        if (isset($this->queuedCache['insert'])) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42 94
            foreach ($this->queuedCache['insert'] as $entity) {
43 94
                $isChanged = $this->updateCache($entity, $isChanged);
44
            }
45
        }
46
47 96 View Code Duplication
        if (isset($this->queuedCache['update'])) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48 6
            foreach ($this->queuedCache['update'] as $entity) {
49 6
                $isChanged = $this->updateCache($entity, $isChanged);
50
            }
51
        }
52
53 96
        if (isset($this->queuedCache['delete'])) {
54 5
            foreach ($this->queuedCache['delete'] as $key) {
55 5
                $this->region->evict($key);
56
57 5
                $isChanged = true;
58
            }
59
        }
60
61 96
        if ($isChanged) {
62 94
            $this->timestampRegion->update($this->timestampKey);
63
        }
64
65 96
        $this->queuedCache = [];
66 96
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 4
    public function afterTransactionRolledBack()
72
    {
73 4
        $this->queuedCache = [];
74 4
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 9
    public function delete($entity)
80
    {
81 9
        $key     = new EntityCacheKey($this->class->rootEntityName, $this->uow->getEntityIdentifier($entity));
82 9
        $deleted = $this->persister->delete($entity);
83
84 9
        if ($deleted) {
85 5
            $this->region->evict($key);
86
        }
87
88 9
        $this->queuedCache['delete'][] = $key;
89
90 9
        return $deleted;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 9
    public function update($entity)
97
    {
98 9
        $this->persister->update($entity);
99
100 9
        $this->queuedCache['update'][] = $entity;
101 9
    }
102
103 95
    private function updateCache($entity, $isChanged)
104
    {
105 95
        $class      = $this->metadataFactory->getMetadataFor(get_class($entity));
106 95
        $key        = new EntityCacheKey($class->rootEntityName, $this->uow->getEntityIdentifier($entity));
0 ignored issues
show
Accessing rootEntityName on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
107 95
        $entry      = $this->hydrator->buildCacheEntry($class, $key, $entity);
0 ignored issues
show
$class of type object<Doctrine\Common\P...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ORM\Mapping\ClassMetadata>. It seems like you assume a concrete implementation of the interface Doctrine\Common\Persistence\Mapping\ClassMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
108 95
        $cached     = $this->region->put($key, $entry);
109 95
        $isChanged  = $isChanged ?: $cached;
110
111 95
        if ($this->cacheLogger && $cached) {
112 93
            $this->cacheLogger->entityCachePut($this->regionName, $key);
113
        }
114
115 95
        return $isChanged;
116
    }
117
}
118