Completed
Push — develop ( 3fec75...18e071 )
by
unknown
07:26
created

setCreationOptions()   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
 * @license MIT
7
 * @copyright  2013 - 2017 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Core\Factory\Form;
12
13
use Core\Form\CustomizableFieldsetInterface;
14
use Interop\Container\ContainerInterface;
15
use Zend\ServiceManager\FactoryInterface;
16
use Zend\ServiceManager\MutableCreationOptionsInterface;
17
use Zend\ServiceManager\ServiceLocatorInterface;
18
19
/**
20
 * Factory boilerplate for customizable Fieldsets
21
 *
22
 * Ideally, you just need to extends this and set the values of the two constants.
23
 * 
24
 * @author Mathias Gelhausen <[email protected]>
25
 * @since 0.29
26
 */
27
abstract class AbstractCustomizableFieldsetFactory implements FactoryInterface, MutableCreationOptionsInterface
28
{
29
    /**
30
     * Name of the options service
31
     * @var string
32
     */
33
    const OPTIONS_NAME = '';
34
35
    /**
36
     * The target fieldset class FQN
37
     *
38
     * @var string
39
     */
40
    const CLASS_NAME   = '';
41
42
    /**
43
     * Creation options.
44
     *
45
     * @var array
46
     */
47
    protected $options;
48
49
    public function setCreationOptions(array $options)
50
    {
51
        $this->options = $options;
52
    }
53
54
    /**
55
     * Reset the creation options.
56
     */
57
    protected function resetCreationOptions()
58
    {
59
        $this->options = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $options.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
60
    }
61
62
    /**
63
     * Creates an instance.
64
     *
65
     * @param ContainerInterface $container
66
     * @param string             $requestedName
67
     * @param array              $options
0 ignored issues
show
Documentation introduced by
Should the type for parameter $options not be null|array?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
68
     *
69
     * @return CustomizableFieldsetInterface
70
     * @throws \RuntimeException
71
     */
72
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
73
    {
74
        /* @var CustomizableFieldsetInterface $instance */
75
        $instance = $this->createFormInstance($container, $requestedName, $options);
76
77
        if (!$instance instanceOf CustomizableFieldsetInterface) {
78
            throw new \RuntimeException('Form or Fieldset instance must implement ' . CustomizableFieldsetInterface::class);
79
        }
80
81
        $customizeOptions = $this->getCustomizationOptions($container, $requestedName, $options);
82
        $instance->setCustomizationOptions($customizeOptions);
83
84
        return $instance;
85
    }
86
87
    /**
88
     * Get the customization options.
89
     *
90
     * @param ContainerInterface $container
91
     * @param string             $requestedName
92
     * @param array              $options
0 ignored issues
show
Documentation introduced by
Should the type for parameter $options not be null|array?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
93
     *
94
     * @return \Core\Options\FieldsetCustomizationOptions
95
     * @throws \RuntimeException
96
     */
97
    protected function getCustomizationOptions(ContainerInterface $container, $requestedName, array $options = null)
0 ignored issues
show
Unused Code introduced by
The parameter $requestedName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
98
    {
99
        if (!static::OPTIONS_NAME) {
100
            throw new \RuntimeException('The class constants "OPTIONS_NAME" must be non empty.');
101
        }
102
103
        return $container->get(static::OPTIONS_NAME);
104
    }
105
106
    /**
107
     * Create the form/fieldset instance
108
     *
109
     * @param ContainerInterface $container
110
     * @param string             $name
111
     * @param array              $options
0 ignored issues
show
Documentation introduced by
Should the type for parameter $options not be null|array?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
112
     *
113
     * @return \Zend\Form\Fieldset
114
     * @throws \RuntimeException
115
     */
116
    protected function createFormInstance(ContainerInterface $container, $name, array $options = null)
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
117
    {
118
        if (!static::CLASS_NAME || !class_exists(static::CLASS_NAME)) {
119
            throw new \RuntimeException('The class constants "CLASS_NAME" must be non empty and name an existent class.');
120
        }
121
122
        $class = static::CLASS_NAME;
123
124
        return new $class(null, $options);
125
    }
126
127
    /**
128
     * Create service
129
     *
130
     * @internal
131
     *      proxies to {@link __invoke()}
132
     *
133
     * @param ServiceLocatorInterface|\Zend\ServiceManager\AbstractPluginManager $serviceLocator
134
     *
135
     * @return CustomizableFieldsetInterface
136
     */
137
    public function createService(ServiceLocatorInterface $serviceLocator)
138
    {
139
        $instance = $this($serviceLocator->getServiceLocator(), '', $this->options);
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...
140
        $this->resetCreationOptions();
141
142
        return $instance;
143
    }
144
}