Completed
Pull Request — master (#1875)
by Andreas
18:19
created

StaticProxyFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ODM\MongoDB\Proxy\Factory;
6
7
use Closure;
8
use Doctrine\Common\EventManager;
9
use Doctrine\Common\NotifyPropertyChanged;
10
use Doctrine\ODM\MongoDB\DocumentManager;
11
use Doctrine\ODM\MongoDB\DocumentNotFoundException;
12
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
13
use Doctrine\ODM\MongoDB\Persisters\DocumentPersister;
14
use Doctrine\ODM\MongoDB\UnitOfWork;
15
use Doctrine\ODM\MongoDB\Utility\LifecycleEventManager;
16
use ProxyManager\Factory\LazyLoadingGhostFactory;
17
use ProxyManager\Proxy\GhostObjectInterface;
18
use ReflectionProperty;
19
use function array_filter;
20
use function count;
21
22
/**
23
 * This factory is used to create proxy objects for documents at runtime.
24
 */
25
class StaticProxyFactory implements ProxyFactory
26
{
27
    /** @var UnitOfWork The UnitOfWork this factory is bound to. */
28
    private $uow;
29
30
    /** @var EventManager */
31
    private $lifecycleEventManager;
32
33
    /** @var LazyLoadingGhostFactory */
34
    private $proxyFactory;
35
36 1634
    public function __construct(DocumentManager $documentManager)
37
    {
38 1634
        $this->uow                   = $documentManager->getUnitOfWork();
39 1634
        $this->lifecycleEventManager = new LifecycleEventManager($documentManager, $this->uow, $documentManager->getEventManager());
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Doctrine\ODM\MongoD...ger->getEventManager()) of type object<Doctrine\ODM\Mong...\LifecycleEventManager> is incompatible with the declared type object<Doctrine\Common\EventManager> of property $lifecycleEventManager.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
40 1634
        $this->proxyFactory          = $documentManager->getConfiguration()->buildGhostObjectFactory();
41 1634
    }
42
43 104
    public function getProxy(ClassMetadata $metadata, $identifier) : GhostObjectInterface
44
    {
45 104
        $documentPersister = $this->uow->getDocumentPersister($metadata->getName());
0 ignored issues
show
Bug introduced by
Consider using $metadata->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
46
47
        $ghostObject = $this
48 104
            ->proxyFactory
49 104
            ->createProxy(
50 104
                $metadata->getName(),
0 ignored issues
show
Bug introduced by
Consider using $metadata->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
51 104
                $this->createInitializer($metadata, $documentPersister),
52
                [
53 104
                    'skippedProperties' => $this->skippedFieldsFqns($metadata),
54
                ]
55
            );
56
57 104
        $metadata->setIdentifierValue($ghostObject, $identifier);
58
59 104
        return $ghostObject;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     *
65
     * @param ClassMetadata[] $classes
66
     */
67
    public function generateProxyClasses(array $classes) : int
68
    {
69
        $concreteClasses = array_filter($classes, static function (ClassMetadata $metadata) : bool {
70
            return ! ($metadata->isMappedSuperclass || $metadata->isQueryResultDocument || $metadata->getReflectionClass()->isAbstract());
71
        });
72
73
        foreach ($concreteClasses as $metadata) {
74
            $this
75
                ->proxyFactory
76
                ->createProxy(
77
                    $metadata->getName(),
78
                    static function () {
79
                        // empty closure, serves its purpose, for now
80
                    },
81
                    $this->skippedFieldsFqns($metadata)
82
                );
83
        }
84
85
        return count($concreteClasses);
86
    }
87
88 104
    private function createInitializer(
89
        ClassMetadata $metadata,
90
        DocumentPersister $documentPersister
91
    ) : Closure {
92
        return function (
93
            GhostObjectInterface $ghostObject,
94
            string $method,
95
            // we don't care
96
            array $parameters,
97
            // we don't care
98
            & $initializer,
99
            array $properties // we currently do not use this
0 ignored issues
show
Unused Code introduced by
The parameter $properties is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100
        ) use (
101 36
            $metadata,
102 36
            $documentPersister
103
        ) : bool {
104 36
            $originalInitializer = $initializer;
105 36
            $initializer         = null;
106 36
            $identifier          = $metadata->getIdentifierValue($ghostObject);
107
108 36
            if (! $documentPersister->load(['_id' => $identifier], $ghostObject)) {
109 9
                $initializer = $originalInitializer;
110
111 9
                if (! $this->lifecycleEventManager->documentNotFound($ghostObject, $identifier)) {
112 8
                    throw DocumentNotFoundException::documentNotFound($metadata->getName(), $identifier);
0 ignored issues
show
Bug introduced by
Consider using $metadata->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
113
                }
114
            }
115
116 30
            if ($ghostObject instanceof NotifyPropertyChanged) {
0 ignored issues
show
Bug introduced by
The class Doctrine\Common\NotifyPropertyChanged does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
117 1
                $ghostObject->addPropertyChangedListener($this->uow);
118
            }
119
120 30
            return true;
121 104
        };
122
    }
123
124
    /**
125
     * @return string[]
126
     */
127 104
    private function skippedFieldsFqns(ClassMetadata $metadata) : array
128
    {
129 104
        $idFieldFqcns = [];
130
131 104
        foreach ($metadata->getIdentifierFieldNames() as $idField) {
132 104
            $idFieldFqcns[] = $this->propertyFqcn($metadata->getReflectionProperty($idField));
133
        }
134
135 104
        return $idFieldFqcns;
136
    }
137
138 104
    private function propertyFqcn(ReflectionProperty $property) : string
139
    {
140 104
        if ($property->isPrivate()) {
141 50
            return "\0" . $property->getDeclaringClass()->getName() . "\0" . $property->getName();
0 ignored issues
show
introduced by
Consider using $property->class. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
142
        }
143
144 60
        if ($property->isProtected()) {
145 22
            return "\0*\0" . $property->getName();
146
        }
147
148 38
        return $property->getName();
149
    }
150
}
151