Completed
Push — develop ( 7f6ad2...eb155e )
by Baptiste
02:38
created

InjectEntityDefinitionsPass   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 5
dl 0
loc 67
ccs 0
cts 43
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 20 3
B computeConfig() 0 31 5
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Neo4jBundle\DependencyInjection\Compiler;
5
6
use Innmind\Neo4jBundle\Exception\NoEntityDefinitionFoundException;
7
use Innmind\Filesystem\{
8
    Adapter\FilesystemAdapter,
9
    Exception\FileNotFoundException,
10
    DirectoryInterface
11
};
12
use Symfony\Component\DependencyInjection\{
13
    Compiler\CompilerPassInterface,
14
    ContainerBuilder
15
};
16
use Symfony\Component\Yaml\Yaml;
17
18
class InjectEntityDefinitionsPass implements CompilerPassInterface
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function process(ContainerBuilder $container)
24
    {
25
        $bundles = $container->getParameter('kernel.bundles');
26
        $configs = [];
27
28
        foreach ($bundles as $bundle => $class) {
29
            try {
30
                $configs[] = $this->computeConfig($class);
31
            } catch (NoEntityDefinitionFoundException $e) {
32
                //pass
33
            }
34
        }
35
36
        $container
37
            ->getDefinition('innmind_neo4j.metadata_builder')
38
            ->addMethodCall(
39
                'inject',
40
                [$configs]
41
            );
42
    }
43
44
    /**
45
     * Load the entity definitions for the given bundle
46
     *
47
     * @param string $class Bundle class FQCN
48
     *
49
     * @throws NoEntityDefinitionFoundException
50
     *
51
     * @return array
52
     */
53
    private function computeConfig(string $class): array
54
    {
55
        try {
56
            $refl = new \ReflectionClass($class);
57
            $dir = (new FilesystemAdapter(dirname($refl->getFileName())))
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Innmind\Filesystem\FileInterface as the method get() does only exist in the following implementations of said interface: Innmind\Filesystem\Directory.

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...
58
                ->get('Resources')
59
                ->get('config');
60
61
            if ($dir->has('neo4j.yml')) {
62
                return Yaml::parse((string) $dir->get('neo4j.yml')->content());
63
            }
64
65
            $dir = $dir->get('neo4j');
66
            $config = [];
67
68
            foreach ($dir as $file) {
69
                if ($file instanceof DirectoryInterface) {
70
                    continue;
71
                }
72
73
                $config = array_merge(
74
                    $config,
75
                    Yaml::parse((string) $file->content())
76
                );
77
            }
78
79
            return $config;
80
        } catch (FileNotFoundException $e) {
81
            throw new NoEntityDefinitionFoundException;
82
        }
83
    }
84
}
85