Failed Conditions
Pull Request — master (#6546)
by Jáchym
11:13
created

InfoCommand::execute()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.7873

Importance

Changes 0
Metric Value
dl 0
loc 35
c 0
b 0
f 0
ccs 13
cts 19
cp 0.6842
rs 8.439
cc 5
eloc 21
nc 9
nop 2
crap 5.7873
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\ORM\Tools\Console\Command;
21
22
use Doctrine\ORM\Mapping\MappingException;
23
use Symfony\Component\Console\Input\InputInterface;
24
use Symfony\Component\Console\Output\OutputInterface;
25
use Symfony\Component\Console\Command\Command;
26
27
/**
28
 * Show information about mapped entities.
29
 *
30
 * @link    www.doctrine-project.org
31
 * @since   2.1
32
 * @author  Benjamin Eberlei <[email protected]>
33
 */
34
class InfoCommand extends Command
35
{
36
    /**
37
     * {@inheritdoc}
38
     */
39 1
    protected function configure()
40
    {
41
        $this
42 1
            ->setName('orm:info')
43 1
            ->setDescription('Show basic information about all mapped entities')
44 1
            ->setHelp(<<<EOT
45 1
The <info>%command.name%</info> shows basic information about which
46
entities exist and possibly if their mapping information contains errors or
47
not.
48
EOT
49
            );
50 1
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 1
    protected function execute(InputInterface $input, OutputInterface $output)
56
    {
57
        /* @var $entityManager \Doctrine\ORM\EntityManager */
58 1
        $entityManager = $this->getHelper('em')->getEntityManager();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Helper\HelperInterface as the method getEntityManager() does only exist in the following implementations of said interface: Doctrine\ORM\Tools\Conso...per\EntityManagerHelper.

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...
59
60 1
        $entityClassNames = $entityManager->getConfiguration()
61 1
                                          ->getMetadataDriverImpl()
62 1
                                          ->getAllClassNames();
63
64 1
        if (!$entityClassNames) {
65
            throw new \Exception(
66
                'You do not have any mapped Doctrine ORM entities according to the current configuration. '.
67
                'If you have entities or mapping files you should check your mapping configuration for errors.'
68
            );
69
        }
70
71 1
        $output->writeln(sprintf("Found <info>%d</info> mapped entities:", count($entityClassNames)));
72
73 1
        $failure = false;
74
75 1
        foreach ($entityClassNames as $entityClassName) {
76
            try {
77 1
                $entityManager->getClassMetadata($entityClassName);
78 1
                $output->writeln(sprintf("<info>[OK]</info>   %s", $entityClassName));
79
            } catch (MappingException $e) {
80
                $output->writeln("<error>[FAIL]</error> ".$entityClassName);
81
                $output->writeln(sprintf("<comment>%s</comment>", $e->getMessage()));
82
                $output->writeln('');
83
84 1
                $failure = true;
85
            }
86
        }
87
88 1
        return $failure ? 1 : 0;
89
    }
90
}
91