Completed
Push — develop ( 49270f...975a05 )
by
unknown
06:55
created

SocialProfilesFieldsetFactory::createService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
/** Auth forms */
11
namespace Auth\Factory\Form;
12
13
use Interop\Container\ContainerInterface;
14
use Zend\ServiceManager\FactoryInterface;
15
use Zend\ServiceManager\ServiceLocatorInterface;
16
use Auth\Form\SocialProfilesFieldset;
17
18
/**
19
 * Factory for a SocialProfilesFieldset
20
 *
21
 * @author Mathias Gelhausen <[email protected]>
22
 */
23
class SocialProfilesFieldsetFactory implements FactoryInterface
24
{
25
    /**
26
     * Creates a {@link SocialProfilesFieldset}
27
     *
28
     * Uses config from the config key [form_element_config][attach_social_profiles_fieldset]
29
     * to configure fetch_url, preview_url and name or uses the defaults:
30
     *  - fetch_url: Route named "auth-social-profiles" with the suffix "?network=%s"
31
     *  - preview_url: Route named "lang/applications/detail" with the suffix "?action=social-profile&network=%s"
32
     *  - name: "social_profiles"
33
     *
34
     * @param  ContainerInterface $container
35
     * @param  string             $requestedName
36
     * @param  null|array         $options
37
     *
38
     * @return object
39
     * @throws ServiceNotFoundException if unable to resolve the service.
40
     * @throws ServiceNotCreatedException if an exception is raised when
41
     *     creating a service.
42
     * @throws ContainerException if any other error occurs
43
     */
44
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
45
    {
46
        /* @var $router         \Zend\Mvc\Router\RouteStackInterface */
47
48
        $router = $container->get('Router');
49
        $config = $container->get('Config');
50
        $options = isset($config['form_element_config']['attach_social_profiles_fieldset'])
51
            ? $config['form_element_config']['attach_social_profiles_fieldset']
52
            : array();
53
54
        if (!isset($options['fetch_url'])) {
55
            $options['fetch_url'] =
56
                $router->assemble(array('action' => 'fetch'), array('name' => 'auth-social-profiles'))
57
                . '?network=%s';
58
        }
59
        if (!isset($options['preview_url'])) {
60
            $options['preview_url'] =
61
                $router->assemble(array('id' => 'null'), array('name' => 'lang/applications/detail'), true)
0 ignored issues
show
Unused Code introduced by
The call to RouteStackInterface::assemble() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
62
                . '?action=social-profile&network=%s';
63
        }
64 View Code Duplication
        if (isset($options['name'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
            $name = $options['name'];
66
            unset($options['name']);
67
        } else {
68
            $name = 'social_profiles';
69
        }
70
        $options['is_disable_capable'] = false;
71
        $options['is_disable_elements_capable'] = false;
72
73
        $fieldset = new SocialProfilesFieldset($name, $options);
74
75
        return $fieldset;
76
    }
77
    
78
    /**
79
     * @param ServiceLocatorInterface $serviceLocator
80
     * @return SocialProfilesFieldset
81
     * @see \Zend\ServiceManager\FactoryInterface::createService()
82
     */
83
    public function createService(ServiceLocatorInterface $serviceLocator)
84
    {
85
        return $this($serviceLocator->getServiceLocator(), SocialProfilesFieldset::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: Acl\Assertion\AssertionManager, Core\Mail\MailService, Core\Paginator\PaginatorService, Zend\Barcode\ObjectPluginManager, Zend\Barcode\RendererPluginManager, Zend\Cache\PatternPluginManager, Zend\Cache\Storage\AdapterPluginManager, Zend\Cache\Storage\PluginManager, Zend\Config\ReaderPluginManager, Zend\Config\WriterPluginManager, Zend\Feed\Reader\ExtensionPluginManager, Zend\Feed\Writer\ExtensionPluginManager, Zend\File\Transfer\Adapter\FilterPluginManager, Zend\File\Transfer\Adapter\ValidatorPluginManager, Zend\Filter\FilterPluginManager, Zend\Form\FormElementMan...lementManagerV2Polyfill, Zend\Form\FormElementMan...lementManagerV3Polyfill, Zend\Hydrator\HydratorPluginManager, Zend\I18n\Translator\LoaderPluginManager, Zend\InputFilter\InputFilterPluginManager, Zend\Log\FilterPluginManager, Zend\Log\FormatterPluginManager, Zend\Log\ProcessorPluginManager, Zend\Log\WriterPluginManager, Zend\Log\Writer\FilterPluginManager, Zend\Log\Writer\FormatterPluginManager, Zend\Mail\Protocol\SmtpPluginManager, Zend\Mvc\Controller\ControllerManager, Zend\Mvc\Controller\PluginManager, Zend\Mvc\Router\RoutePluginManager, Zend\Paginator\AdapterPluginManager, Zend\Paginator\ScrollingStylePluginManager, Zend\Serializer\AdapterPluginManager, Zend\ServiceManager\AbstractPluginManager, Zend\Stdlib\Hydrator\HydratorPluginManager, Zend\Tag\Cloud\DecoratorPluginManager, Zend\Text\Table\DecoratorManager, 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...
86
    }
87
}
88