Completed
Push — master ( 9b1160...9e63e2 )
by Walter
03:15
created

EngineFactory::loadTestVariantEventManager()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 22
rs 8.9197
cc 4
eloc 14
nc 4
nop 3
1
<?php
2
3
namespace PhpAbModule\Service;
4
5
use PhpAb\Engine\Engine;
6
use PhpAb\Engine\EngineInterface;
7
use PhpAb\Event\DispatcherInterface;
8
use PhpAb\Participation\ParticipationManagerInterface;
9
use PhpAb\Test\Test;
10
use PhpAb\Variant\CallbackVariant;
11
use PhpAb\Variant\SimpleVariant;
12
use PhpAbModule\Variant\EventManagerVariant;
13
use RuntimeException;
14
use Zend\EventManager\EventManager;
15
use Zend\ServiceManager\FactoryInterface;
16
use Zend\ServiceManager\ServiceLocatorInterface;
17
18
class EngineFactory implements FactoryInterface
19
{
20
    public function createService(ServiceLocatorInterface $serviceLocator)
21
    {
22
        /** @var array $config */
23
        $config = $serviceLocator->get('Config');
24
25
        /** @var ParticipationManagerInterface $participationManager */
26
        $participationManager = $serviceLocator->get('phpab.participation_manager');
27
28
        /** @var DispatcherInterface $dispatcher */
29
        $dispatcher = $serviceLocator->get('phpab.dispatcher');
30
31
        $filter = $this->loadService($serviceLocator, $config['phpab']['default_filter']);
32
        $chooser = $this->loadService($serviceLocator, $config['phpab']['default_variant_chooser']);
33
34
        $engine = new Engine($participationManager, $dispatcher, $filter, $chooser);
0 ignored issues
show
Bug introduced by
It seems like $filter defined by $this->loadService($serv...ab']['default_filter']) on line 31 can also be of type array or object; however, PhpAb\Engine\Engine::__construct() does only seem to accept null|object<PhpAb\Participation\FilterInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $chooser defined by $this->loadService($serv...ault_variant_chooser']) on line 32 can also be of type array or object; however, PhpAb\Engine\Engine::__construct() does only seem to accept null|object<PhpAb\Variant\ChooserInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
35
36
        $this->loadTests($engine, $serviceLocator, $config['phpab']);
37
38
        return $engine;
39
    }
40
41
    private function loadService(ServiceLocatorInterface $serviceLocator, $serviceName)
42
    {
43
        if (!$serviceLocator->has($serviceName)) {
44
            return null;
45
        }
46
47
        return $serviceLocator->get($serviceName);
48
    }
49
50
    private function loadTests(EngineInterface $engine, ServiceLocatorInterface $serviceLocator, $config)
51
    {
52
        foreach ($config['tests'] as $identifier => $testConfig) {
53
            $this->loadTest($serviceLocator, $engine, $identifier, $testConfig);
54
        }
55
    }
56
57
    private function loadTest(ServiceLocatorInterface $serviceLocator, EngineInterface $engine, $identifier, array $config)
58
    {
59
        $filter = null;
60
        if (array_key_exists('filter', $config)) {
61
            $filter = $serviceLocator->get($config['filter']);
62
        }
63
64
        $variantChooser = null;
65
        if (array_key_exists('variant_chooser', $config)) {
66
            $variantChooser = $serviceLocator->get($config['variant_chooser']);
67
        }
68
69
        $variants = $this->loadTestVariants($serviceLocator, $config);
70
        $options = array_key_exists('options', $config) ? $config['options'] : [];
71
72
        $test = new Test($identifier, $variants);
73
74
        $engine->addTest($test, $options, $filter, $variantChooser);
0 ignored issues
show
Unused Code introduced by
The call to EngineInterface::addTest() has too many arguments starting with $filter.

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...
75
    }
76
77
    private function loadTestVariants(ServiceLocatorInterface $serviceLocator, $config)
78
    {
79
        $variants = [];
80
81
        foreach ($config['variants'] as $identifier => $variant) {
82
83
            // We also support shortcuts so that the user can specify a service name straight away.
84
            if (is_string($variant)) {
85
                $variant = [
86
                    'type' => $variant,
87
                ];
88
            }
89
90
            if (!array_key_exists('type', $variant)) {
91
                throw new RuntimeException('The type of the variant is missing.');
92
            }
93
94
            $options = empty($variant['options']) ? [] : $variant['options'];
95
96
            $variants[] = $this->loadTestVariant($serviceLocator, $variant['type'], $identifier, $options);
97
        }
98
99
        return $variants;
100
    }
101
102
    private function loadTestVariant(ServiceLocatorInterface $serviceLocator, $type, $identifier, array $options)
103
    {
104
        switch ($type) {
105
            case 'callback':
106
                $variant = $this->loadTestVariantCallback($serviceLocator, $identifier, $options);
107
                break;
108
109
            case 'event_manager':
110
                $variant = $this->loadTestVariantEventManager($serviceLocator, $identifier, $options);
111
                break;
112
113
            case 'simple':
114
                $variant = $this->loadTestVariantSimple($serviceLocator, $identifier, $options);
115
                break;
116
117
            case 'service_manager':
118
            default:
119
                $variant = $this->loadTestVariantFromServiceManager($serviceLocator, $type);
120
                break;
121
        }
122
123
        return $variant;
124
    }
125
126
    private function loadTestVariantCallback(ServiceLocatorInterface $serviceLocator, $identifier, array $options)
0 ignored issues
show
Unused Code introduced by
The parameter $serviceLocator 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...
127
    {
128
        if (!array_key_exists('callback', $options)) {
129
            throw new RuntimeException('Missing "callback" for callback variant.');
130
        }
131
132
        if (!is_callable($options['callback'])) {
133
            throw new RuntimeException('The "callback" for callback variant cannot be called.');
134
        }
135
136
        return new CallbackVariant($identifier, $options['callback']);
137
    }
138
139
    private function loadTestVariantEventManager(ServiceLocatorInterface $serviceLocator, $identifier, array $options)
140
    {
141
        if (empty($options['event_manager'])) {
142
            $eventManager = $serviceLocator->get('Application')->getEventManager();
143
        } else {
144
            $eventManager = $serviceLocator->get($options['event_manager']);
145
        }
146
147
        $callback = $options['callback'];
148
149
        if (!is_callable($callback) && $serviceLocator->has($callback)) {
150
            $callback = $serviceLocator->get($callback);
151
        }
152
153
        return new EventManagerVariant(
154
            $eventManager,
155
            $identifier,
156
            $options['event'],
157
            $callback,
158
            $options['priority']
159
        );
160
    }
161
162
    private function loadTestVariantSimple(ServiceLocatorInterface $serviceLocator, $identifier, array $options)
0 ignored issues
show
Unused Code introduced by
The parameter $serviceLocator 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...
163
    {
164
        return new SimpleVariant($identifier);
165
    }
166
167
    private function loadTestVariantFromServiceManager(ServiceLocatorInterface $serviceLocator, $type)
168
    {
169
        if (!$serviceLocator->has($type)) {
170
            throw new RuntimeException(sprintf('The variant "%s" is not a valid service manager name.', $type));
171
        }
172
173
        return $serviceLocator->get($type);
174
    }
175
}
176