Completed
Push — master ( 65b001...744dd5 )
by Westin
05:31
created

src/FlySystemManagerFactory.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
declare(strict_types=1);
3
4
namespace WShafer\PSR11FlySystem;
5
6
use Psr\Container\ContainerInterface;
7
use WShafer\PSR11FlySystem\Adaptor\AdaptorMapper;
8
use WShafer\PSR11FlySystem\Cache\CacheMapper;
9
use WShafer\PSR11FlySystem\Config\MainConfig;
10
use WShafer\PSR11FlySystem\Service\AdaptorManager;
11
use WShafer\PSR11FlySystem\Service\CacheManager;
12
13
class FlySystemManagerFactory
14
{
15
    protected $config;
16
17 1
    public function __invoke(ContainerInterface $container)
18
    {
19
        /** @var MainConfig $config */
20 1
        $config = $this->getConfig($container);
21
22
        /** @var AdaptorManager $adaptorManager */
23 1
        $adaptorManager = $this->getAdaptorManager($container);
24
25
        /** @var CacheManager $cacheManager */
26 1
        $cacheManager = $this->getCacheManager($container);
27
28 1
        return new FlySystemManager($config, $adaptorManager, $cacheManager, $container);
29
    }
30
31 7
    public function getConfig(ContainerInterface $container)
32
    {
33 7
        if (!$this->config) {
34 7
            $config = $this->getConfigArray($container);
35 7
            $this->config = new MainConfig($config);
36
        }
37
38 6
        return $this->config;
39
    }
40
41 7
    protected function getConfigArray(ContainerInterface $container)
42
    {
43
        // Symfony config is parameters. //
44 7
        if (method_exists($container, 'getParameter')
45 7
            && method_exists($container, 'hasParameter')
46 7
            && $container->hasParameter('flysystem')
1 ignored issue
show
It seems like you code against a concrete implementation and not the interface Psr\Container\ContainerInterface as the method hasParameter() does only exist in the following implementations of said interface: Container14\ProjectServiceContainer, ProjectServiceContainer, Symfony\Component\DependencyInjection\Container, Symfony\Component\Depend...ection\ContainerBuilder, Symfony\Component\Depend...tainers\CustomContainer, Symfony\Component\Depend...ProjectServiceContainer, Symfony\Component\Depend...ProjectServiceContainer.

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...
47
        ) {
48 1
            return ['flysystem' => $container->getParameter('flysystem')];
1 ignored issue
show
It seems like you code against a concrete implementation and not the interface Psr\Container\ContainerInterface as the method getParameter() does only exist in the following implementations of said interface: Container14\ProjectServiceContainer, ProjectServiceContainer, Symfony\Component\DependencyInjection\Container, Symfony\Component\Depend...ection\ContainerBuilder, Symfony\Component\Depend...tainers\CustomContainer, Symfony\Component\Depend...ProjectServiceContainer, Symfony\Component\Depend...ProjectServiceContainer.

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...
49
        }
50
51
        // Zend uses config key
52 6
        if ($container->has('config')) {
53 4
            return $container->get('config');
54
        }
55
56
        // Slim Config comes from "settings"
57 2
        if ($container->has('settings')) {
58 1
            return ['flysystem' => $container->get('settings')['flysystem']];
59
        }
60
61 1
        return [];
62
    }
63
64 2
    public function getAdaptorManager(ContainerInterface $container)
65
    {
66
        /** @var MainConfig $config */
67 2
        $config = $this->getConfig($container);
68
69
        /** @var AdaptorMapper $adaptorMapper */
70 2
        $adaptorMapper = $this->getAdaptorMapper($container);
71
72 2
        return new AdaptorManager($config, $adaptorMapper, $container);
73
    }
74
75 3
    public function getAdaptorMapper(ContainerInterface $container)
76
    {
77 3
        return new AdaptorMapper($container);
78
    }
79
80 2
    public function getCacheManager(ContainerInterface $container)
81
    {
82
        /** @var MainConfig $config */
83 2
        $config = $this->getConfig($container);
84
85
        /** @var CacheMapper $cacheMapper */
86 2
        $cacheMapper = $this->getCacheMapper($container);
87
88 2
        return new CacheManager($config, $cacheMapper, $container);
89
    }
90
91 3
    public function getCacheMapper(ContainerInterface $container)
92
    {
93 3
        return new CacheMapper($container);
94
    }
95
}
96