Completed
Push — master ( 02d26c...304629 )
by Walter
03:17
created

EngineFactory::loadService()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
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 $key => $testConfig) {
51
            $this->loadTest($serviceLocator, $engine, $testConfig);
52
        }
53
    }
54
55
    private function loadTest(ServiceLocatorInterface $serviceLocator, EngineInterface $engine, array $config)
56
    {
57
        if (!array_key_exists('identifier', $config)) {
58
            throw new RuntimeException('Missing the "identifier" for the test.');
59
        }
60
61
        $filter = null;
62
        if (array_key_exists('filter', $config)) {
63
            $filter = $serviceLocator->get($config['filter']);
64
        }
65
66
        $variantChooser = null;
67
        if (array_key_exists('variant_chooser', $config)) {
68
            $variantChooser = $serviceLocator->get($config['variant_chooser']);
69
        }
70
71
        $variants = $this->loadTestVariants($serviceLocator, $config);
72
        $options = array_key_exists('options', $config) ? $config['options'] : [];
73
74
        $test = new Test($config['identifier'], $variants);
75
76
        $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...
77
    }
78
79
    private function loadTestVariants(ServiceLocatorInterface $serviceLocator, $config)
80
    {
81
        $variants = [];
82
83
        foreach ($config['variants'] as $variant) {
84
            if (!array_key_exists('type', $variant)) {
85
                throw new RuntimeException('The type of the variant is missing.');
86
            }
87
88
            $options = empty($variant['options']) ? [] : $variant['options'];
89
90
            $variants[] = $this->loadTestVariant($serviceLocator, $variant['type'], $options);
91
        }
92
93
        return $variants;
94
    }
95
96
    private function loadTestVariant(ServiceLocatorInterface $serviceLocator, $type, array $options)
97
    {
98
        switch ($type) {
99
            case 'simple':
100
                $variant = $this->loadTestVariantSimple($serviceLocator, $options);
101
                break;
102
103
            case 'callback':
104
                $variant = $this->loadTestVariantCallback($serviceLocator, $options);
105
                break;
106
107
            default:
108
                $variant = $this->loadTestVariantFromServiceManager($serviceLocator, $type);
109
                break;
110
        }
111
112
        return $variant;
113
    }
114
115
    private function loadTestVariantCallback(ServiceLocatorInterface $serviceLocator, 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...
116
    {
117
        if (!array_key_exists('identifier', $options)) {
118
            throw new RuntimeException('Missing "identifier" for callback variant.');
119
        }
120
121
        if (!array_key_exists('callback', $options)) {
122
            throw new RuntimeException('Missing "callback" for callback variant.');
123
        }
124
125
        if (!is_callable($options['callback'])) {
126
            throw new RuntimeException('The "callback" for callback variant cannot be called.');
127
        }
128
129
        return new CallbackVariant($options['identifier'], $options['callback']);
130
    }
131
132
    private function loadTestVariantSimple(ServiceLocatorInterface $serviceLocator, 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...
133
    {
134
        if (!array_key_exists('identifier', $options)) {
135
            throw new RuntimeException('Missing "identifier" for simple variant.');
136
        }
137
138
        return new SimpleVariant($options['identifier']);
139
    }
140
141
    private function loadTestVariantFromServiceManager(ServiceLocatorInterface $serviceLocator, $type)
142
    {
143
        if (!$serviceLocator->has($type)) {
144
            throw new RuntimeException(sprintf('The variant "%s" could not be loaded.', $type));
145
        }
146
147
        return $serviceLocator->get($type);
148
    }
149
}
150