Completed
Pull Request — master (#15)
by Vytautas
10:02
created

SettingsApiControllerFactory::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Svycka\Settings\Controller\Factory;
4
5
use Interop\Container\ContainerInterface;
6
use Svycka\Settings\Collection\CollectionsManager;
7
use Svycka\Settings\Controller\SettingsApiController;
8
use Zend\ServiceManager\FactoryInterface;
9
use Zend\ServiceManager\ServiceLocatorInterface;
10
11
/**
12
 * @author Vytautas Stankus <[email protected]>
13
 * @license MIT
14
 */
15
class SettingsApiControllerFactory implements FactoryInterface
16
{
17 1
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
18
    {
19
        /** @var CollectionsManager $manager */
20 1
        $manager = $container->get(CollectionsManager::class);
21
22 1
        return new SettingsApiController($manager);
23
    }
24
25
    /**
26
     * Create service
27
     *
28
     * @param ServiceLocatorInterface $serviceLocator
29
     *
30
     * @return SettingsApiController
31
     */
32
    public function createService(ServiceLocatorInterface $serviceLocator)
33
    {
34
        return $this($serviceLocator->getServiceLocator(), SettingsApiController::class);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\ServiceManager\ServiceLocatorInterface as the method getServiceLocator() does only exist in the following implementations of said interface: Svycka\Settings\Collection\CollectionsManager, Svycka\Settings\Type\TypesManager, Zend\Cache\PatternPluginManager, Zend\Cache\Storage\AdapterPluginManager, Zend\Cache\Storage\PluginManager, Zend\Config\ReaderPluginManager, Zend\Config\WriterPluginManager, Zend\Filter\FilterPluginManager, Zend\Form\FormElementMan...lementManagerV2Polyfill, Zend\Form\FormElementMan...lementManagerV3Polyfill, Zend\Hydrator\HydratorPluginManager, Zend\I18n\Translator\LoaderPluginManager, Zend\InputFilter\InputFilterPluginManager, Zend\Mvc\Controller\ControllerManager, Zend\Mvc\Controller\PluginManager, Zend\Mvc\Router\RoutePluginManager, Zend\Paginator\AdapterPluginManager, Zend\Paginator\ScrollingStylePluginManager, Zend\ServiceManager\AbstractPluginManager, Zend\Stdlib\Hydrator\HydratorPluginManager, Zend\Validator\ValidatorPluginManager, Zend\View\HelperPluginManager, Zend\View\Helper\Navigation\PluginManager.

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...
35
    }
36
}
37