RedoGetLatLngFromGoogleUsingAddressTask::run()   C
last analyzed

Complexity

Conditions 11
Paths 168

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 6.469
c 0
b 0
f 0
cc 11
nc 168
nop 1

How to fix   Long Method    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 RedoGetLatLngFromGoogleUsingAddressTask extends BuildTask
4
{
5
    protected $title = "Re-attempt to convert address into google map co-ordinates";
6
7
    protected $description = "Runs through all the failed GetLatLngFromGoogleUsingAddress";
8
9
    /**
10
     * @var boolean
11
     */
12
    protected $verbose = true;
13
14
    /**
15
     * @param boolean
16
     */
17
    public function setVerbose($b)
18
    {
19
        $this->verbose = $b;
20
    }
21
22
    private $step = 100;
23
24
    public function run($request)
25
    {
26
        if ($this->verbose) {
27
            set_time_limit(3600);
28
            increase_time_limit_to(3600);
29
            increase_memory_limit_to('512M');
30
            Config::inst()->update("GetLatLngFromGoogleUsingAddress", "debug", true);
31
        }
32
        if ($this->verbose) {
33
            echo "<h1>============= Start =============</h1>";
34
            flush();
35
            ob_flush();
36
        }
37
        if ($this->verbose) {
38
            $objects = GoogleMapLocationsObject::get()->where("(Longitude IS NULL OR Latitude IS NULL) AND (Address IS NOT NULL AND Address <> '')")->count();
0 ignored issues
show
Unused Code introduced by
$objects 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...
39
            echo "<h3>============= Redoing $count Objects =============</h3>";
0 ignored issues
show
Bug introduced by
The variable $count does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
40
            flush();
41
            ob_flush();
42
        }
43
        for ($i = 0; $i < 1000000; $i = $i + $this->step) {
44
            $objects = GoogleMapLocationsObject::get()->where("(Longitude IS NULL OR Latitude IS NULL) AND (Address IS NOT NULL AND Address <> '')")->limit($this->step, $i);
0 ignored issues
show
Unused Code introduced by
$objects 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...
45
            if ($objects = GoogleMapLocationsObject::get()->where("(Longitude IS NULL OR Latitude IS NULL) AND (Address IS NOT NULL AND Address <> '')")->limit($this->step, $i)->count() == 0) {
46
                $i = 99999999999;
47
            }
48
            foreach ($objects as $object) {
0 ignored issues
show
Bug introduced by
The expression $objects of type boolean is not traversable.
Loading history...
49
                DB::alteration_message("<hr /><hr /><hr /><h1>Looking up ".$object->Address."</h1>");
50
                $object->FullAddress = '';
51
                $object->write();
52
                if ($object->Longitude) {
53
                    if ($this->verbose) {
54
                        DB::alteration_message(" --- found ".$object->FullAddress, "created");
55
                        ;
56
                        flush();
57
                        ob_flush();
58
                    }
59
                } else {
60
                    if ($this->verbose) {
61
                        DB::alteration_message(" --- not found ".$object->Address, "deleted");
62
                        ;
63
                        flush();
64
                        ob_flush();
65
                    }
66
                }
67
            }
68
            if ($this->verbose) {
69
                echo "<h1>============= END =============</h1>";
70
                flush();
71
                ob_flush();
72
            }
73
        }
74
    }
75
}
76