Completed
Push — master ( bf3814...9b1160 )
by Walter
03:46
created

EngineFactory::loadTestVariants()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 24
rs 8.5125
cc 5
eloc 11
nc 7
nop 2
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 RuntimeException;
13
use Zend\ServiceManager\FactoryInterface;
14
use Zend\ServiceManager\ServiceLocatorInterface;
15
16
class EngineFactory implements FactoryInterface
17
{
18
    public function createService(ServiceLocatorInterface $serviceLocator)
19
    {
20
        /** @var array $config */
21
        $config = $serviceLocator->get('Config');
22
23
        /** @var ParticipationManagerInterface $participationManager */
24
        $participationManager = $serviceLocator->get('phpab.participation_manager');
25
26
        /** @var DispatcherInterface $dispatcher */
27
        $dispatcher = $serviceLocator->get('phpab.dispatcher');
28
29
        $filter = $this->loadService($serviceLocator, $config['phpab']['default_filter']);
30
        $chooser = $this->loadService($serviceLocator, $config['phpab']['default_variant_chooser']);
31
32
        $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 29 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 30 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...
33
34
        $this->loadTests($engine, $serviceLocator, $config['phpab']);
35
36
        return $engine;
37
    }
38
39
    private function loadService(ServiceLocatorInterface $serviceLocator, $serviceName)
40
    {
41
        if (!$serviceLocator->has($serviceName)) {
42
            return null;
43
        }
44
45
        return $serviceLocator->get($serviceName);
46
    }
47
48
    private function loadTests(EngineInterface $engine, ServiceLocatorInterface $serviceLocator, $config)
49
    {
50
        foreach ($config['tests'] as $identifier => $testConfig) {
51
            $this->loadTest($serviceLocator, $engine, $identifier, $testConfig);
52
        }
53
    }
54
55
    private function loadTest(ServiceLocatorInterface $serviceLocator, EngineInterface $engine, $identifier, array $config)
56
    {
57
        $filter = null;
58
        if (array_key_exists('filter', $config)) {
59
            $filter = $serviceLocator->get($config['filter']);
60
        }
61
62
        $variantChooser = null;
63
        if (array_key_exists('variant_chooser', $config)) {
64
            $variantChooser = $serviceLocator->get($config['variant_chooser']);
65
        }
66
67
        $variants = $this->loadTestVariants($serviceLocator, $config);
68
        $options = array_key_exists('options', $config) ? $config['options'] : [];
69
70
        $test = new Test($identifier, $variants);
71
72
        $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...
73
    }
74
75
    private function loadTestVariants(ServiceLocatorInterface $serviceLocator, $config)
76
    {
77
        $variants = [];
78
79
        foreach ($config['variants'] as $identifier => $variant) {
80
81
            // We also support shortcuts so that the user can specify a service name straight away.
82
            if (is_string($variant)) {
83
                $variant = [
84
                    'type' => $variant,
85
                ];
86
            }
87
88
            if (!array_key_exists('type', $variant)) {
89
                throw new RuntimeException('The type of the variant is missing.');
90
            }
91
92
            $options = empty($variant['options']) ? [] : $variant['options'];
93
94
            $variants[] = $this->loadTestVariant($serviceLocator, $variant['type'], $identifier, $options);
95
        }
96
97
        return $variants;
98
    }
99
100
    private function loadTestVariant(ServiceLocatorInterface $serviceLocator, $type, $identifier, array $options)
101
    {
102
        switch ($type) {
103
            case 'callback':
104
                $variant = $this->loadTestVariantCallback($serviceLocator, $identifier, $options);
105
                break;
106
107
            case 'simple':
108
                $variant = $this->loadTestVariantSimple($serviceLocator, $identifier, $options);
109
                break;
110
111
            case 'service_manager':
112
            default:
113
                $variant = $this->loadTestVariantFromServiceManager($serviceLocator, $type);
114
                break;
115
        }
116
117
        return $variant;
118
    }
119
120
    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...
121
    {
122
        if (!array_key_exists('callback', $options)) {
123
            throw new RuntimeException('Missing "callback" for callback variant.');
124
        }
125
126
        if (!is_callable($options['callback'])) {
127
            throw new RuntimeException('The "callback" for callback variant cannot be called.');
128
        }
129
130
        return new CallbackVariant($identifier, $options['callback']);
131
    }
132
133
    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...
134
    {
135
        return new SimpleVariant($identifier);
136
    }
137
138
    private function loadTestVariantFromServiceManager(ServiceLocatorInterface $serviceLocator, $type)
139
    {
140
        if (!$serviceLocator->has($type)) {
141
            throw new RuntimeException(sprintf('The variant "%s" is not a valid service manager name.', $type));
142
        }
143
144
        return $serviceLocator->get($type);
145
    }
146
}
147