Completed
Push — master ( 61f8e0...b1cbaf )
by Maciej
13:57
created

XmlDriver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ODM\MongoDB\Mapping\Driver;
21
22
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
23
use Doctrine\Common\Persistence\Mapping\Driver\FileDriver;
24
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata as MappingClassMetadata;
25
use Doctrine\ODM\MongoDB\Utility\CollectionHelper;
26
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
27
28
/**
29
 * XmlDriver is a metadata driver that enables mapping through XML files.
30
 *
31
 * @since       1.0
32
 */
33
class XmlDriver extends FileDriver
34
{
35
    const DEFAULT_FILE_EXTENSION = '.dcm.xml';
36
37
    /**
38
     * {@inheritDoc}
39
     */
40 12
    public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENSION)
41
    {
42 12
        parent::__construct($locator, $fileExtension);
43 12
    }
44
45
    /**
46
     * {@inheritDoc}
47
     */
48 8
    public function loadMetadataForClass($className, ClassMetadata $class)
49
    {
50
        /* @var $class ClassMetadataInfo */
51
        /* @var $xmlRoot \SimpleXMLElement */
52 8
        $xmlRoot = $this->getElement($className);
53 8
        if ( ! $xmlRoot) {
54
            return;
55
        }
56
57 8
        if ($xmlRoot->getName() == 'document') {
58 8
            if (isset($xmlRoot['repository-class'])) {
59
                $class->setCustomRepositoryClass((string) $xmlRoot['repository-class']);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Common\Persistence\Mapping\ClassMetadata as the method setCustomRepositoryClass() does only exist in the following implementations of said interface: Doctrine\ODM\MongoDB\Mapping\ClassMetadata, Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo.

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
60
            }
61 8 View Code Duplication
        } elseif ($xmlRoot->getName() == 'mapped-superclass') {
0 ignored issues
show
Duplication introduced by
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...
62 2
            $class->setCustomRepositoryClass(
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Common\Persistence\Mapping\ClassMetadata as the method setCustomRepositoryClass() does only exist in the following implementations of said interface: Doctrine\ODM\MongoDB\Mapping\ClassMetadata, Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo.

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
63 2
                isset($xmlRoot['repository-class']) ? (string) $xmlRoot['repository-class'] : null
64 2
            );
65 2
            $class->isMappedSuperclass = true;
0 ignored issues
show
Bug introduced by
Accessing isMappedSuperclass 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...
66 3
        } elseif ($xmlRoot->getName() == 'embedded-document') {
67 1
            $class->isEmbeddedDocument = true;
0 ignored issues
show
Bug introduced by
Accessing isEmbeddedDocument 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...
68 1
        }
69 8
        if (isset($xmlRoot['db'])) {
70 4
            $class->setDatabase((string) $xmlRoot['db']);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Common\Persistence\Mapping\ClassMetadata as the method setDatabase() does only exist in the following implementations of said interface: Doctrine\ODM\MongoDB\Mapping\ClassMetadata, Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo.

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
71 4
        }
72 8
        if (isset($xmlRoot['collection'])) {
73 7
            if (isset($xmlRoot['capped-collection'])) {
74
                $config = array('name' => (string) $xmlRoot['collection']);
75
                $config['capped'] = (bool) $xmlRoot['capped-collection'];
76
                if (isset($xmlRoot['capped-collection-max'])) {
77
                    $config['max'] = (int) $xmlRoot['capped-collection-max'];
78
                }
79
                if (isset($xmlRoot['capped-collection-size'])) {
80
                    $config['size'] = (int) $xmlRoot['capped-collection-size'];
81
                }
82
                $class->setCollection($config);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Common\Persistence\Mapping\ClassMetadata as the method setCollection() does only exist in the following implementations of said interface: Doctrine\ODM\MongoDB\Mapping\ClassMetadata, Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo.

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
83
            } else {
84 7
                $class->setCollection((string) $xmlRoot['collection']);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Common\Persistence\Mapping\ClassMetadata as the method setCollection() does only exist in the following implementations of said interface: Doctrine\ODM\MongoDB\Mapping\ClassMetadata, Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo.

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
85
            }
86 7
        }
87 8
        if (isset($xmlRoot['inheritance-type'])) {
88
            $inheritanceType = (string) $xmlRoot['inheritance-type'];
89
            $class->setInheritanceType(constant(MappingClassMetadata::class . '::INHERITANCE_TYPE_' . $inheritanceType));
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Common\Persistence\Mapping\ClassMetadata as the method setInheritanceType() does only exist in the following implementations of said interface: Doctrine\ODM\MongoDB\Mapping\ClassMetadata, Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo.

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
90
        }
91 8 View Code Duplication
        if (isset($xmlRoot['change-tracking-policy'])) {
0 ignored issues
show
Duplication introduced by
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...
92 2
            $class->setChangeTrackingPolicy(constant(MappingClassMetadata::class . '::CHANGETRACKING_' . strtoupper((string) $xmlRoot['change-tracking-policy'])));
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Common\Persistence\Mapping\ClassMetadata as the method setChangeTrackingPolicy() does only exist in the following implementations of said interface: Doctrine\ODM\MongoDB\Mapping\ClassMetadata, Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo.

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
93 2
        }
94 8
        if (isset($xmlRoot->{'discriminator-field'})) {
95 1
            $discrField = $xmlRoot->{'discriminator-field'};
96
            /* XSD only allows for "name", which is consistent with association
97
             * configurations, but fall back to "fieldName" for BC.
98
             */
99 1
            $class->setDiscriminatorField(
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Common\Persistence\Mapping\ClassMetadata as the method setDiscriminatorField() does only exist in the following implementations of said interface: Doctrine\ODM\MongoDB\Mapping\ClassMetadata, Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo.

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
100 1
                isset($discrField['name']) ? (string) $discrField['name'] : (string) $discrField['fieldName']
101 1
            );
102 1
        }
103 8 View Code Duplication
        if (isset($xmlRoot->{'discriminator-map'})) {
0 ignored issues
show
Duplication introduced by
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...
104 1
            $map = array();
105 1
            foreach ($xmlRoot->{'discriminator-map'}->{'discriminator-mapping'} AS $discrMapElement) {
106 1
                $map[(string) $discrMapElement['value']] = (string) $discrMapElement['class'];
107 1
            }
108 1
            $class->setDiscriminatorMap($map);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Common\Persistence\Mapping\ClassMetadata as the method setDiscriminatorMap() does only exist in the following implementations of said interface: Doctrine\ODM\MongoDB\Mapping\ClassMetadata, Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo.

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
109 1
        }
110 8
        if (isset($xmlRoot->{'default-discriminator-value'})) {
111 1
            $class->setDefaultDiscriminatorValue((string) $xmlRoot->{'default-discriminator-value'}['value']);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Common\Persistence\Mapping\ClassMetadata as the method setDefaultDiscriminatorValue() does only exist in the following implementations of said interface: Doctrine\ODM\MongoDB\Mapping\ClassMetadata, Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo.

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
112 1
        }
113 8
        if (isset($xmlRoot->{'indexes'})) {
114 3
            foreach ($xmlRoot->{'indexes'}->{'index'} as $index) {
115 3
                $this->addIndex($class, $index);
0 ignored issues
show
Compatibility introduced by
$class of type object<Doctrine\Common\P...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ODM\Mong...ping\ClassMetadataInfo>. 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...
116 3
            }
117 3
        }
118 8
        if (isset($xmlRoot['require-indexes'])) {
119 1
            $class->setRequireIndexes('true' === (string) $xmlRoot['require-indexes']);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Common\Persistence\Mapping\ClassMetadata as the method setRequireIndexes() does only exist in the following implementations of said interface: Doctrine\ODM\MongoDB\Mapping\ClassMetadata, Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo.

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
120 1
        }
121 8
        if (isset($xmlRoot['slave-okay'])) {
122 1
            $class->setSlaveOkay('true' === (string) $xmlRoot['slave-okay']);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Common\Persistence\Mapping\ClassMetadata as the method setSlaveOkay() does only exist in the following implementations of said interface: Doctrine\ODM\MongoDB\Mapping\ClassMetadata, Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo.

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
123 1
        }
124 8
        if (isset($xmlRoot->field)) {
125 8
            foreach ($xmlRoot->field as $field) {
126 8
                $mapping = array();
127 8
                $attributes = $field->attributes();
128 8
                foreach ($attributes as $key => $value) {
129 8
                    $mapping[$key] = (string) $value;
130 8
                    $booleanAttributes = array('id', 'reference', 'embed', 'unique', 'sparse', 'file', 'distance');
131 8
                    if (in_array($key, $booleanAttributes)) {
132 8
                        $mapping[$key] = ('true' === $mapping[$key]);
133 8
                    }
134 8
                }
135 8
                if (isset($mapping['id']) && $mapping['id'] === true && isset($mapping['strategy'])) {
136 3
                    $mapping['options'] = array();
137 3
                    if (isset($field->{'id-generator-option'})) {
138 1
                        foreach ($field->{'id-generator-option'} as $generatorOptions) {
139 1
                            $attributesGenerator = iterator_to_array($generatorOptions->attributes());
140 1
                            if (isset($attributesGenerator['name']) && isset($attributesGenerator['value'])) {
141 1
                                $mapping['options'][(string) $attributesGenerator['name']] = (string) $attributesGenerator['value'];
142 1
                            }
143 1
                        }
144 1
                    }
145 3
                }
146
147 8
                if (isset($attributes['not-saved'])) {
148
                    $mapping['notSaved'] = ('true' === (string) $attributes['not-saved']);
149
                }
150
151 8
                if (isset($attributes['also-load'])) {
152
                    $mapping['alsoLoadFields'] = explode(',', $attributes['also-load']);
153 8
                } elseif (isset($attributes['version'])) {
154 1
                    $mapping['version'] = ('true' === (string) $attributes['version']);
155 8
                } elseif (isset($attributes['lock'])) {
156 1
                    $mapping['lock'] = ('true' === (string) $attributes['lock']);
157 1
                }
158
159 8
                $this->addFieldMapping($class, $mapping);
0 ignored issues
show
Compatibility introduced by
$class of type object<Doctrine\Common\P...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ODM\Mong...ping\ClassMetadataInfo>. 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...
160 8
            }
161 8
        }
162 8
        if (isset($xmlRoot->{'embed-one'})) {
163 2
            foreach ($xmlRoot->{'embed-one'} as $embed) {
164 2
                $this->addEmbedMapping($class, $embed, 'one');
0 ignored issues
show
Compatibility introduced by
$class of type object<Doctrine\Common\P...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ODM\Mong...ping\ClassMetadataInfo>. 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...
165 2
            }
166 2
        }
167 8
        if (isset($xmlRoot->{'embed-many'})) {
168 2
            foreach ($xmlRoot->{'embed-many'} as $embed) {
169 2
                $this->addEmbedMapping($class, $embed, 'many');
0 ignored issues
show
Compatibility introduced by
$class of type object<Doctrine\Common\P...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ODM\Mong...ping\ClassMetadataInfo>. 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...
170 2
            }
171 2
        }
172 8
        if (isset($xmlRoot->{'reference-many'})) {
173 3
            foreach ($xmlRoot->{'reference-many'} as $reference) {
174 3
                $this->addReferenceMapping($class, $reference, 'many');
0 ignored issues
show
Compatibility introduced by
$class of type object<Doctrine\Common\P...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ODM\Mong...ping\ClassMetadataInfo>. 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...
175 3
            }
176 3
        }
177 8
        if (isset($xmlRoot->{'reference-one'})) {
178 3
            foreach ($xmlRoot->{'reference-one'} as $reference) {
179 3
                $this->addReferenceMapping($class, $reference, 'one');
0 ignored issues
show
Compatibility introduced by
$class of type object<Doctrine\Common\P...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ODM\Mong...ping\ClassMetadataInfo>. 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...
180 3
            }
181 3
        }
182 8
        if (isset($xmlRoot->{'lifecycle-callbacks'})) {
183 2
            foreach ($xmlRoot->{'lifecycle-callbacks'}->{'lifecycle-callback'} as $lifecycleCallback) {
184 2
                $class->addLifecycleCallback((string) $lifecycleCallback['method'], constant('Doctrine\ODM\MongoDB\Events::' . (string) $lifecycleCallback['type']));
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Common\Persistence\Mapping\ClassMetadata as the method addLifecycleCallback() does only exist in the following implementations of said interface: Doctrine\ODM\MongoDB\Mapping\ClassMetadata, Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo.

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
185 2
            }
186 2
        }
187 8
        if (isset($xmlRoot->{'also-load-methods'})) {
188 1
            foreach ($xmlRoot->{'also-load-methods'}->{'also-load-method'} as $alsoLoadMethod) {
189 1
                $class->registerAlsoLoadMethod((string) $alsoLoadMethod['method'], (string) $alsoLoadMethod['field']);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Common\Persistence\Mapping\ClassMetadata as the method registerAlsoLoadMethod() does only exist in the following implementations of said interface: Doctrine\ODM\MongoDB\Mapping\ClassMetadata, Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo.

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
190 1
            }
191 1
        }
192 8
    }
193
194 8
    private function addFieldMapping(ClassMetadataInfo $class, $mapping)
195
    {
196 8 View Code Duplication
        if (isset($mapping['name'])) {
0 ignored issues
show
Duplication introduced by
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...
197 8
            $name = $mapping['name'];
198 8
        } elseif (isset($mapping['fieldName'])) {
199 1
            $name = $mapping['fieldName'];
200 1
        } else {
201
            throw new \InvalidArgumentException('Cannot infer a MongoDB name from the mapping');
202
        }
203
204 8
        $class->mapField($mapping);
205
206
        // Index this field if either "index", "unique", or "sparse" are set
207 8 View Code Duplication
        if ( ! (isset($mapping['index']) || isset($mapping['unique']) || isset($mapping['sparse']))) {
0 ignored issues
show
Duplication introduced by
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...
208 8
            return;
209
        }
210
211 2
        $keys = array($name => isset($mapping['order']) ? $mapping['order'] : 'asc');
212 2
        $options = array();
213
214 2
        if (isset($mapping['background'])) {
215
            $options['background'] = (boolean) $mapping['background'];
216
        }
217 2
        if (isset($mapping['drop-dups'])) {
218 1
            $options['dropDups'] = (boolean) $mapping['drop-dups'];
219 1
        }
220 2
        if (isset($mapping['index-name'])) {
221
            $options['name'] = (string) $mapping['index-name'];
222
        }
223 2
        if (isset($mapping['safe'])) {
224
            $options['safe'] = (boolean) $mapping['safe'];
225
        }
226 2
        if (isset($mapping['sparse'])) {
227 1
            $options['sparse'] = (boolean) $mapping['sparse'];
228 1
        }
229 2
        if (isset($mapping['unique'])) {
230 2
            $options['unique'] = (boolean) $mapping['unique'];
231 2
        }
232
233 2
        $class->addIndex($keys, $options);
234 2
    }
235
236 2
    private function addEmbedMapping(ClassMetadataInfo $class, $embed, $type)
237
    {
238 2
        $attributes = $embed->attributes();
239 2
        $defaultStrategy = $type == 'one' ? ClassMetadataInfo::STORAGE_STRATEGY_SET : CollectionHelper::DEFAULT_STRATEGY;
240
        $mapping = array(
241 2
            'type'           => $type,
242 2
            'embedded'       => true,
243 2
            'targetDocument' => isset($attributes['target-document']) ? (string) $attributes['target-document'] : null,
244 2
            'name'           => (string) $attributes['field'],
245 2
            'strategy'       => isset($attributes['strategy']) ? (string) $attributes['strategy'] : $defaultStrategy,
246 2
        );
247 2
        if (isset($attributes['fieldName'])) {
248 1
            $mapping['fieldName'] = (string) $attributes['fieldName'];
249 1
        }
250 2 View Code Duplication
        if (isset($embed->{'discriminator-field'})) {
0 ignored issues
show
Duplication introduced by
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...
251 1
            $attr = $embed->{'discriminator-field'};
252 1
            $mapping['discriminatorField'] = (string) $attr['name'];
253 1
        }
254 2 View Code Duplication
        if (isset($embed->{'discriminator-map'})) {
0 ignored issues
show
Duplication introduced by
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...
255 1
            foreach ($embed->{'discriminator-map'}->{'discriminator-mapping'} as $discriminatorMapping) {
256 1
                $attr = $discriminatorMapping->attributes();
257 1
                $mapping['discriminatorMap'][(string) $attr['value']] = (string) $attr['class'];
258 1
            }
259 1
        }
260 2
        if (isset($embed->{'default-discriminator-value'})) {
261 1
            $mapping['defaultDiscriminatorValue'] = (string) $embed->{'default-discriminator-value'}['value'];
262 1
        }
263 2
        if (isset($attributes['not-saved'])) {
264
            $mapping['notSaved'] = ('true' === (string) $attributes['not-saved']);
265
        }
266 2
        if (isset($attributes['also-load'])) {
267
            $mapping['alsoLoadFields'] = explode(',', $attributes['also-load']);
268
        }
269 2
        $this->addFieldMapping($class, $mapping);
270 2
    }
271
272 3
    private function addReferenceMapping(ClassMetadataInfo $class, $reference, $type)
273
    {
274 3
        $cascade = array_keys((array) $reference->cascade);
275 3
        if (1 === count($cascade)) {
276 2
            $cascade = current($cascade) ?: next($cascade);
277 2
        }
278 3
        $attributes = $reference->attributes();
279 3
        $defaultStrategy = $type == 'one' ? ClassMetadataInfo::STORAGE_STRATEGY_SET : CollectionHelper::DEFAULT_STRATEGY;
280
        $mapping = array(
281 3
            'cascade'          => $cascade,
282 3
            'orphanRemoval'    => isset($attributes['orphan-removal']) ? ('true' === (string) $attributes['orphan-removal']) : false,
283 3
            'type'             => $type,
284 3
            'reference'        => true,
285 3
            'simple'           => isset($attributes['simple']) ? ('true' === (string) $attributes['simple']) : false,
286 3
            'targetDocument'   => isset($attributes['target-document']) ? (string) $attributes['target-document'] : null,
287 3
            'name'             => (string) $attributes['field'],
288 3
            'strategy'         => isset($attributes['strategy']) ? (string) $attributes['strategy'] : $defaultStrategy,
289 3
            'inversedBy'       => isset($attributes['inversed-by']) ? (string) $attributes['inversed-by'] : null,
290 3
            'mappedBy'         => isset($attributes['mapped-by']) ? (string) $attributes['mapped-by'] : null,
291 3
            'repositoryMethod' => isset($attributes['repository-method']) ? (string) $attributes['repository-method'] : null,
292 3
            'limit'            => isset($attributes['limit']) ? (integer) $attributes['limit'] : null,
293 3
            'skip'             => isset($attributes['skip']) ? (integer) $attributes['skip'] : null,
294 3
        );
295
296 3
        if (isset($attributes['fieldName'])) {
297 1
            $mapping['fieldName'] = (string) $attributes['fieldName'];
298 1
        }
299 3 View Code Duplication
        if (isset($reference->{'discriminator-field'})) {
0 ignored issues
show
Duplication introduced by
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...
300 1
            $attr = $reference->{'discriminator-field'};
301 1
            $mapping['discriminatorField'] = (string) $attr['name'];
302 1
        }
303 3 View Code Duplication
        if (isset($reference->{'discriminator-map'})) {
0 ignored issues
show
Duplication introduced by
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...
304 1
            foreach ($reference->{'discriminator-map'}->{'discriminator-mapping'} as $discriminatorMapping) {
305 1
                $attr = $discriminatorMapping->attributes();
306 1
                $mapping['discriminatorMap'][(string) $attr['value']] = (string) $attr['class'];
307 1
            }
308 1
        }
309 3
        if (isset($reference->{'default-discriminator-value'})) {
310 1
            $mapping['defaultDiscriminatorValue'] = (string) $reference->{'default-discriminator-value'}['value'];
311 1
        }
312 3
        if (isset($reference->{'sort'})) {
313 View Code Duplication
            foreach ($reference->{'sort'}->{'sort'} as $sort) {
0 ignored issues
show
Duplication introduced by
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...
314
                $attr = $sort->attributes();
315
                $mapping['sort'][(string) $attr['field']] = isset($attr['order']) ? (string) $attr['order'] : 'asc';
316
            }
317
        }
318 3
        if (isset($reference->{'criteria'})) {
319 View Code Duplication
            foreach ($reference->{'criteria'}->{'criteria'} as $criteria) {
0 ignored issues
show
Duplication introduced by
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...
320
                $attr = $criteria->attributes();
321
                $mapping['criteria'][(string) $attr['field']] = (string) $attr['value'];
322
            }
323
        }
324 3
        if (isset($attributes['not-saved'])) {
325
            $mapping['notSaved'] = ('true' === (string) $attributes['not-saved']);
326
        }
327 3
        if (isset($attributes['also-load'])) {
328
            $mapping['alsoLoadFields'] = explode(',', $attributes['also-load']);
329
        }
330 3
        $this->addFieldMapping($class, $mapping);
331 3
    }
332
333 3
    private function addIndex(ClassMetadataInfo $class, \SimpleXmlElement $xmlIndex)
334
    {
335 3
        $attributes = $xmlIndex->attributes();
336
337 3
        $keys = array();
338
339 3
        foreach ($xmlIndex->{'key'} as $key) {
340 3
            $keys[(string) $key['name']] = isset($key['order']) ? (string) $key['order'] : 'asc';
341 3
        }
342
343 3
        $options = array();
344
345 3
        if (isset($attributes['background'])) {
346
            $options['background'] = ('true' === (string) $attributes['background']);
347
        }
348 3
        if (isset($attributes['drop-dups'])) {
349
            $options['dropDups'] = ('true' === (string) $attributes['drop-dups']);
350
        }
351 3
        if (isset($attributes['name'])) {
352
            $options['name'] = (string) $attributes['name'];
353
        }
354 3
        if (isset($attributes['safe'])) {
355
            $options['safe'] = ('true' === (string) $attributes['safe']);
356
        }
357 3
        if (isset($attributes['sparse'])) {
358
            $options['sparse'] = ('true' === (string) $attributes['sparse']);
359
        }
360 3
        if (isset($attributes['unique'])) {
361 1
            $options['unique'] = ('true' === (string) $attributes['unique']);
362 1
        }
363
364 3
        if (isset($xmlIndex->{'option'})) {
365 1
            foreach ($xmlIndex->{'option'} as $option) {
366 1
                $value = (string) $option['value'];
367 1 View Code Duplication
                if ($value === 'true') {
0 ignored issues
show
Duplication introduced by
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...
368
                    $value = true;
369 1
                } elseif ($value === 'false') {
370 1
                    $value = false;
371 1
                } elseif (is_numeric($value)) {
372 1
                    $value = preg_match('/^[-]?\d+$/', $value) ? (integer) $value : (float) $value;
373 1
                }
374 1
                $options[(string) $option['name']] = $value;
375 1
            }
376 1
        }
377
378 3
        if (isset($xmlIndex->{'partial-filter-expression'})) {
379 3
            $partialFilterExpressionMapping = $xmlIndex->{'partial-filter-expression'};
380
381 3
            if (isset($partialFilterExpressionMapping->and)) {
382 2
                foreach ($partialFilterExpressionMapping->and as $and) {
383 2
                    if (! isset($and->field)) {
384 1
                        continue;
385
                    }
386
387 2
                    $partialFilterExpression = $this->getPartialFilterExpression($and->field);
388 2
                    if (! $partialFilterExpression) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $partialFilterExpression of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
389
                        continue;
390
                    }
391
392 2
                    $options['partialFilterExpression']['$and'][] = $partialFilterExpression;
393 2
                }
394 3
            } elseif (isset($partialFilterExpressionMapping->field)) {
395 2
                $partialFilterExpression = $this->getPartialFilterExpression($partialFilterExpressionMapping->field);
396
397 2
                if ($partialFilterExpression) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $partialFilterExpression of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
398 2
                    $options['partialFilterExpression'] = $partialFilterExpression;
399 2
                }
400 2
            }
401 3
        }
402
403 3
        $class->addIndex($keys, $options);
404 3
    }
405
406 3
    private function getPartialFilterExpression(\SimpleXMLElement $fields)
407
    {
408 3
        $partialFilterExpression = [];
409 3
        foreach ($fields as $field) {
410 3
            $operator = (string) $field['operator'] ?: null;
411
412 3
            if (! isset($field['value'])) {
413 1
                if (! isset($field->field)) {
414
                    continue;
415
                }
416
417 1
                $nestedExpression = $this->getPartialFilterExpression($field->field);
418 1
                if (! $nestedExpression) {
419
                    continue;
420
                }
421
422 1
                $value = $nestedExpression;
423 1
            } else {
424 3
                $value = trim((string) $field['value']);
425
            }
426
427 3 View Code Duplication
            if ($value === 'true') {
0 ignored issues
show
Duplication introduced by
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...
428
                $value = true;
429 3
            } elseif ($value === 'false') {
430
                $value = false;
431 3
            } elseif (is_numeric($value)) {
432 2
                $value = preg_match('/^[-]?\d+$/', $value) ? (integer) $value : (float) $value;
433 2
            }
434
435 3
            $partialFilterExpression[(string) $field['name']] = $operator ? ['$' . $operator => $value] : $value;
436 3
        }
437
438 3
        return $partialFilterExpression;
439
    }
440
441
    /**
442
     * {@inheritDoc}
443
     */
444 8
    protected function loadMappingFile($file)
445
    {
446 8
        $result = array();
447 8
        $xmlElement = simplexml_load_file($file);
448
449 8
        foreach (array('document', 'embedded-document', 'mapped-superclass') as $type) {
450 8
            if (isset($xmlElement->$type)) {
451 8
                foreach ($xmlElement->$type as $documentElement) {
452 8
                    $documentName = (string) $documentElement['name'];
453 8
                    $result[$documentName] = $documentElement;
454 8
                }
455 8
            }
456 8
        }
457
458 8
        return $result;
459
    }
460
}
461