RegisterDefinitionFilesPass::process()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 1
crap 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Rest\ServerBundle\DependencyInjection\Compiler;
5
6
use Innmind\Filesystem\{
7
    Adapter\FilesystemAdapter,
8
    Directory,
9
    Exception\FileNotFound
10
};
11
use Innmind\Immutable\{
12
    Set,
13
    SetInterface
14
};
15
use Symfony\Component\DependencyInjection\{
16
    ContainerBuilder,
17
    Compiler\CompilerPassInterface
18
};
19
20
final class RegisterDefinitionFilesPass implements CompilerPassInterface
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25 46
    public function process(ContainerBuilder $container)
26
    {
27 46
        $bundles = $container->getParameter('kernel.bundles');
28 46
        $files = new Set('string');
29
30 46
        foreach ($bundles as $bundle => $class) {
31 38
            $files = $files->merge(
32 38
                $this->searchFiles($class)
33
            );
34
        }
35
36
        $container
37 46
            ->getDefinition('innmind_rest_server.definition.directories')
38 46
            ->addArgument($files->toPrimitive());
39 46
    }
40
41
    /**
42
     * @param string $class
43
     * @return SetInterface<string>
0 ignored issues
show
Documentation introduced by
The doc-type SetInterface<string> could not be parsed: Expected "|" or "end of type", but got "<" at position 12. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
44
     */
45 38
    private function searchFiles(string $class): SetInterface
46
    {
47 38
        $files = new Set('string');
48
49
        try {
50 38
            $refl = new \ReflectionClass($class);
51 38
            $path = dirname($refl->getFileName());
52 38
            $config = (new FilesystemAdapter($path))
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Innmind\Filesystem\File as the method get() does only exist in the following implementations of said interface: Innmind\Filesystem\Directory\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...
53 38
                ->get('Resources')
54 38
                ->get('config');
55 38
            $path .= '/Resources/config';
56
57 38
            if ($config->has('rest.yml')) {
58 38
                return $files->add($path.'/rest.yml');
59
            }
60
61 2
            $folder = $config->get('rest');
62
63 2
            foreach ($folder as $file) {
64 2
                if ($file instanceof Directory) {
65 2
                    continue;
66
                }
67
68 2
                $files = $files->add(
69 2
                    $path.'/rest/'.$file->name()
70
                );
71
            }
72 2
        } catch (FileNotFound $e) {
73
            //pass
74
        }
75
76 2
        return $files;
77
    }
78
}
79