BuildTaskTestCache::run()   D
last analyzed

Complexity

Conditions 10
Paths 14

Size

Total Lines 43
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 32
nc 14
nop 1
dl 0
loc 43
rs 4.8196
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
class BuildTaskTestCache extends BuildTask
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    protected $title = 'Test Silverstripe Cache';
6
7
    protected $description = '
8
        Basic test for the Sillverstripe Cache.
9
        It will show the date and time the cache was made.';
10
11
    public function run($request)
0 ignored issues
show
Coding Style introduced by
run uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
12
    {
13
        $cache = SS_Cache::factory('foo');
14
        $result = $cache->load('bar');
15
        if (!$result || isset($_GET['reload'])) {
16
            $time = time();
17
            for ($i = 1; $i < $time; $i = $i + 75) {
18
                $temp = $time / $time - rand(0, 10);
0 ignored issues
show
Unused Code introduced by
$temp is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
19
            }
20
            $result = date('Y-m-d H:i:s');
21
            ;
22
            DB::alteration_message('not from cache: '.$result, 'deleted');
23
            $cache->save($result, 'bar');
24
        } else {
25
            DB::alteration_message('from cache: '.$result, 'created');
26
        }
27
28
        if (isset($_GET['setm'])) {
29
            foreach (array('11211', '11212', '11213', '11214') as $port) {
30
                echo "<h1>SETTING: $port</h1>";
31
                $memcache = new Memcache;
32
                $cacheAvailable = $memcache->connect('127.0.0.1', $port);
33
                if ($cacheAvailable) {
34
                    $memcache->set('test_memcache', 'set at: '.date('Y-m-d H:i:s'));
35
                    echo "SET";
36
                } else {
37
                    echo "NOT SET";
38
                }
39
            }
40
        } elseif (isset($_GET['getm'])) {
41
            foreach (array('11211', '11212', '11213', '11214') as $port) {
42
                echo "<h1>GETTING: $port</h1>";
43
                $memcache = new Memcache;
44
                $cacheAvailable = $memcache->connect('127.0.0.1', $port);
45
                if ($cacheAvailable) {
46
                    $outcome = $memcache->get('test_memcache');
47
                    echo $outcome;
48
                } else {
49
                    echo "COULD NOT GET";
50
                }
51
            }
52
        }
53
    }
54
}
55