Passed
Push — master ( bae6ce...385af2 )
by Tim
01:33
created

Store::parsePhpMemcachedConfiguration()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 5
nop 1
dl 0
loc 25
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
namespace SimpleSAML\Module\monitor\TestSuite;
4
5
use \SimpleSAML\Module\monitor\TestConfiguration as TestConfiguration;
6
use \SimpleSAML\Module\monitor\State as State;
7
use \SimpleSAML\Module\monitor\TestCase as TestCase;
8
use \SimpleSAML\Module\monitor\TestData as TestData;
9
use \SimpleSAML\Logger as Logger;
10
11
final class Store extends \SimpleSAML\Module\monitor\TestSuiteFactory
12
{
13
    /**
14
     * var string|null
15
     */
16
    private $store = null;
17
18
    /**
19
     * @param TestConfiguration $configuration
20
     */
21
    public function __construct($configuration)
22
    {
23
        $globalConfig = $configuration->getGlobalConfig();
24
        $this->store = $globalConfig->getString('store.type', 'phpsession');
25
        $this->setCategory('Session store');
26
27
        parent::__construct($configuration);
28
    }
29
30
    /**
31
     * @return void
32
     */
33
    public function invokeTest()
34
    {
35
        $configuration = $this->getConfiguration();
36
37
        switch ($this->store) {
38
            case 'phpsession':
39
                switch (ini_get('session.save_handler')) {
40
                    case 'files':
41
                        $input = [
42
                            'path' => session_save_path(),
43
                            'category' => 'Session storage'
44
                        ];
45
                        $testData = new TestData($input);
46
                        $test = new TestCase\FileSystem\FreeSpace($testData);
47
                        $results = array($test->getTestResult());
48
                        break;
49
                    case 'memcache':
50
                    case 'memcached':
51
                        $configuration = \SimpleSAML_Configuration::setPreLoadedConfig(
0 ignored issues
show
Bug introduced by
The method setPreLoadedConfig() does not exist on SimpleSAML_Configuration. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
                        /** @scrutinizer ignore-call */ 
52
                        $configuration = \SimpleSAML_Configuration::setPreLoadedConfig(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
52
                            \SimpleSAML_Configuration::loadFromArray(
53
                                array(
54
                                    'memcache_store.servers' => $this->parsePhpMemcachedConfiguration(session_save_path())
55
                                )
56
                            )
57
                        );
58
59
                        $test = new Store\Memcache($configuration);
60
                        $results = $test->getTestResults();
61
                        break;
62
// TODO:
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
63
//                    case 'sqlite':
64
//                    case 'mm':
65
                    default:
66
                        Logger::warning("Not implemented;  $this->store - Skipping Store TestSuite.");
67
                        return;
68
                }
69
                break;
70
            case 'memcache':
71
                $test = new Store\Memcache($configuration);
72
                $results = $test->getTestResults();
73
                break;
74
// TODO:
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
75
//            case 'redis':
76
//            case 'redissentinel':
77
//                $test = new Store\Redis($configuration);
78
//                break;
79
            case 'sql':
80
                $test = new Store\Sql($configuration);
81
                $results = $test->getTestResults();
82
                break;
83
            default:
84
                Logger::warning("Not implemented;  $this->store - Skipping Store TestSuite.");
85
                return;
86
87
        }
88
        foreach ($results as $result) {
89
            $this->addTestResult($result);
90
        }
91
        $this->calculateState();
92
    }
93
94
    /**
95
     * @param string $spec
96
     *
97
     * @return \SimpleSAML\Configuration
0 ignored issues
show
Bug introduced by
The type SimpleSAML\Configuration was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
98
     */
99
    private function parsePhpMemcachedConfiguration($spec)
100
    {
101
        $servers = preg_split('/\s*,\s*/', $spec);
102
103
        $results = array();
104
        foreach ($servers as $server) {
105
            $result = array();
106
            list($host, $params) = explode('?', $server);
107
            list($hostname, $port) = explode(':', $host);
108
109
            // Strip protocol when possible (memcache)
110
            $prefix = 'tcp://';
111
            if (substr($hostname, 0, 6) === $prefix) {
112
                $hostname = substr($hostname, 6);
113
            }
114
115
            $result['hostname'] = $hostname;
116
            if ($port !== null) {
117
                $result['port'] = $port;
118
            }
119
            parse_str($params, $tmp);
120
            $results[]  = array_merge($result, $tmp);
121
        }
122
123
        return array($results);
0 ignored issues
show
Bug Best Practice introduced by
The expression return array($results) returns the type array<integer,array<mixed,array>|array> which is incompatible with the documented return type SimpleSAML\Configuration.
Loading history...
124
    }
125
}
126