Intraface_modules_invoice_Pdf_Reminder::visit()   F
last analyzed

Complexity

Conditions 24
Paths 16001

Size

Total Lines 186
Code Lines 124

Duplication

Lines 52
Ratio 27.96 %

Code Coverage

Tests 0
CRAP Score 600

Importance

Changes 0
Metric Value
cc 24
eloc 124
nc 16001
nop 1
dl 52
loc 186
ccs 0
cts 145
cp 0
crap 600
rs 2
c 0
b 0
f 0

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
 * Invoice
4
 *
5
 * @package Intraface_Invoice
6
 * @author Sune Jensen <[email protected]>
7
 * @author Lars Olesen <[email protected]>
8
 */
9
class Intraface_modules_invoice_Pdf_Reminder extends Intraface_modules_debtor_Pdf
10
{
11
    function __construct($translation, $file = null)
12
    {
13
        parent::__construct($translation, $file);
14
    }
15
16
    function visit($reminder)
17
    {
18
        if ($reminder->get('id') == 0) {
19
            throw new Exception("Reminder->pdf must be loaded to create a PDF");
20
        }
21
22
        $this->doc = $this->getDocument();
23
24 View Code Duplication
        if (!empty($this->file) and $this->file->get('id') > 0) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
25
            $this->doc->addHeader($this->file->get('file_uri_pdf'));
26
        }
27
28
        $contact = $reminder->contact->address->get();
29
        if (isset($reminder->contact_person) and get_class($reminder->contact_person) == "contactperson") {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
30
            $contact["attention_to"] = $reminder->contact_person->get("name");
31
        }
32
        $contact['number'] = $reminder->contact->get('number');
33
34
        $intranet_address = new Intraface_Address($reminder->get("intranet_address_id"));
35
        $intranet = $intranet_address->get();
36
37
        switch ($reminder->kernel->setting->get('intranet', 'debtor.sender')) {
38
            case 'intranet':
39
                // void
40
                break;
41
            case 'user':
42
                $intranet['email'] = $reminder->kernel->user->getAddress()->get('email');
43
                $intranet['contact_person'] = $reminder->kernel->user->getAddress()->get('name');
44
                $intranet['phone'] = $reminder->kernel->user->getAddress()->get('phone');
45
                break;
46 View Code Duplication
            case 'defined':
47
                $intranet['email'] = $reminder->kernel->setting->get('intranet', 'debtor.sender.email');
48
                $intranet['contact_person'] = $reminder->kernel->setting->get('intranet', 'debtor.sender.name');
49
                break;
50
        }
51
52
        $this->docinfo[0]["label"] = "Dato:";
0 ignored issues
show
Bug introduced by
The property docinfo does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
53
        $this->docinfo[0]["value"] = $reminder->get("dk_this_date");
54
55
        $this->addRecieverAndSender($contact, $intranet, "Reminder about payment", $this->docinfo);
56
57
        $this->doc->setY('-20'); // space to the product list
58
59
        $text = explode("\r\n", $reminder->get("text"));
60 View Code Duplication
        foreach ($text as $line) {
61
            if ($line == "") {
62
                $this->doc->setY('-'.$this->doc->get('font_spacing'));
63
64
                if ($this->doc->get('y') < $this->doc->get("margin_bottom") + $this->doc->get("font_spacing") * 2) {
65
                    $this->doc->nextPage(true);
66
                }
67
            } else {
68
                while ($line != "") {
69
                    $this->doc->setY('-'.($this->doc->get("font_padding_top") + $this->doc->get("font_size")));
70
                    $line = $this->doc->addTextWrap($this->doc->get('x'), $this->doc->get('y'), $this->doc->get("right_margin_position") - $this->doc->get('x'), $this->doc->get("font_size"), $line); // $this->doc->get("right_margin_position") - $this->doc->get('x')
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
71
              
72
                    $this->doc->setY('-'.$this->doc->get("font_padding_bottom"));
73
74
                    if ($this->doc->get('y') < $this->doc->get("margin_bottom") + $this->doc->get("font_spacing") * 2) {
75
                        $this->doc->nextPage(true);
76
                    }
77
                }
78
            }
79
        }
80
81
        // Headlines for products
82
83
        $this->doc->setY('-20'); // space to product list
84
85
        if ($this->doc->get('y') < $this->doc->get("margin_bottom") + $this->doc->get("font_spacing") * 3) {
86
            $this->doc->nextPage(true);
87
        }
88
89
        $apointX["text"] = $this->doc->get("margin_left");
0 ignored issues
show
Coding Style Comprehensibility introduced by
$apointX was never initialized. Although not strictly required by PHP, it is generally a good practice to add $apointX = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
90
        $apointX["invoice_date"] = $this->doc->get("right_margin_position") - 225;
91
        $apointX["due_date"] = $this->doc->get("right_margin_position") - 150;
92
        $apointX["amount"] = $this->doc->get("right_margin_position");
93
        $apointX["text_width"] = $this->doc->get("right_margin_position") - $this->doc->get("margin_left") - $apointX["text"] - 60;
94
95
96
        $this->doc->addText($apointX["text"], $this->doc->get('y'), $this->doc->get("font_size"), "Beskrivelse");
97
        $this->doc->addText($apointX["invoice_date"], $this->doc->get('y'), $this->doc->get("font_size"), "Dato");
98
        $this->doc->addText($apointX["due_date"], $this->doc->get('y'), $this->doc->get("font_size"), "Forfaldsdato");
99
        $this->doc->addText($apointX["amount"] - $this->doc->getTextWidth($this->doc->get("font_size"), "Beløb") -3, $this->doc->get('y'), $this->doc->get("font_size"), "Bel�b");
100
101
        $this->doc->setY('-'.($this->doc->get("font_spacing") - $this->doc->get("font_size")));
102
103
        $this->doc->line($this->doc->get("margin_left"), $this->doc->get('y'), $this->doc->get("right_margin_position"), $this->doc->get('y'));
104
105
        // vareoversigt
106
107
        $reminder->loadItem();
108
        $items = $reminder->item->getList("invoice");
109
110
        $total = 0;
111
        $color = 0;
112
113
        for ($i = 0, $max = count($items); $i < $max; $i++) {
114 View Code Duplication
            if ($color == 1) {
115
                $this->doc->setColor(0.8, 0.8, 0.8);
116
                $this->doc->filledRectangle($this->doc->get("margin_left"), $this->doc->get('y') - $this->doc->get("font_spacing"), $this->doc->get("right_margin_position") - $this->doc->get("margin_left"), $this->doc->get("font_spacing"));
117
                $this->doc->setColor(0, 0, 0);
118
                $color = 0;
119
            } else {
120
                $color = 1;
121
            }
122
123
            $this->doc->setY('-'.($this->doc->get("font_size") + $this->doc->get("font_padding_top")));
124
125
            $this->doc->addText($apointX["text"], $this->doc->get('y'), $this->doc->get("font_size"), "Faktura nr. ".$items[$i]["number"]);
126
            $this->doc->addText($apointX["invoice_date"], $this->doc->get('y'), $this->doc->get("font_size"), $items[$i]["dk_this_date"]);
127
            $this->doc->addText($apointX["due_date"], $this->doc->get('y'), $this->doc->get("font_size"), $items[$i]["dk_due_date"]);
128
            $this->doc->addText($apointX["amount"] - $this->doc->getTextWidth($this->doc->get("font_size"), number_format($items[$i]["arrears"], 2, ",", ".")), $this->doc->get('y'), $this->doc->get("font_size"), number_format($items[$i]["arrears"], 2, ",", "."));
129
            $this->doc->setY('-'.$this->doc->get("font_padding_bottom"));
130
            $total += $items[$i]["arrears"];
131
132
            if ($this->doc->get('y') < $this->doc->get("margin_bottom") + $this->doc->get("font_spacing") * 2) {
133
                $this->doc->nextPage(true);
134
            }
135
        }
136
137
        $items = $reminder->item->getList("reminder");
138
139
        for ($i = 0, $max = count($items); $i < $max; $i++) {
140 View Code Duplication
            if ($color == 1) {
141
                $this->doc->setColor(0.8, 0.8, 0.8);
142
                $this->doc->filledRectangle($this->doc->get("margin_left"), $this->doc->get('y') - $this->doc->get("font_spacing"), $this->doc->get("right_margin_position") - $this->doc->get("margin_left"), $this->doc->get("font_spacing"));
143
                $this->doc->setColor(0, 0, 0);
144
                $color = 0;
145
            } else {
146
                $color = 1;
147
            }
148
149
            $this->doc->setY('-'.($this->doc->get("font_size") + $this->doc->get("font_padding_top")));
150
            $this->doc->addText($apointX["text"], $this->doc->get('y'), $this->doc->get("font_size"), "Rykkkergebyr fra tidligere rykker");
151
            $this->doc->addText($apointX["invoice_date"], $this->doc->get('y'), $this->doc->get("font_size"), $items[$i]["dk_this_date"]);
152
            $this->doc->addText($apointX["due_date"], $this->doc->get('y'), $this->doc->get("font_size"), $items[$i]["dk_due_date"]);
153
            $this->doc->addText($apointX["amount"] - $this->doc->getTextWidth($this->doc->get("font_size"), number_format($items[$i]["reminder_fee"], 2, ",", ".")), $this->doc->get('y'), $this->doc->get("font_size"), number_format($items[$i]["reminder_fee"], 2, ",", "."));
154
            $this->doc->setY('-'.$this->doc->get("font_padding_bottom"));
155
            $total += $items[$i]["reminder_fee"];
156
157
            if ($this->doc->get('y') < $this->doc->get("margin_bottom") + $this->doc->get("font_spacing") * 2) {
158
                $this->doc->nextPage(true);
159
            }
160
        }
161
162
        if ($reminder->get("reminder_fee") > 0) {
163 View Code Duplication
            if ($color == 1) {
164
                $this->doc->setColor(0.8, 0.8, 0.8);
165
                $this->doc->filledRectangle($this->doc->get("margin_left"), $this->doc->get('y') - $this->doc->get("font_spacing"), $this->doc->get("right_margin_position") - $this->doc->get("margin_left"), $this->doc->get("font_spacing"));
166
                $this->doc->setColor(0, 0, 0);
167
                $color = 0;
0 ignored issues
show
Unused Code introduced by
$color 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...
168
            } else {
169
                $color = 1;
0 ignored issues
show
Unused Code introduced by
$color 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...
170
            }
171
172
            $this->doc->setY('-'.($this->doc->get("font_size") + $this->doc->get("font_padding_top")));
173
            $this->doc->addText($apointX["text"], $this->doc->get('y'), $this->doc->get("font_size"), "Rykkergebyr p�lagt denne rykker");
174
            $this->doc->addText($apointX["amount"] - $this->doc->getTextWidth($this->doc->get("font_size"), number_format($reminder->get("reminder_fee"), 2, ",", ".")), $this->doc->get('y'), $this->doc->get("font_size"), number_format($reminder->get("reminder_fee"), 2, ",", "."));
175
            $this->doc->setY('-'.$this->doc->get("font_padding_bottom"));
176
            $total += $reminder->get("reminder_fee");
177
178
            if ($this->doc->get('y') < $this->doc->get("margin_bottom") + $this->doc->get("font_spacing") * 2) {
179
                $this->doc->nextPage(true);
180
            }
181
        }
182
183
        $this->doc->setLineStyle(1);
184
        $this->doc->line($this->doc->get("margin_left"), $this->doc->get('y'), $this->doc->get("right_margin_position"), $this->doc->get('y'));
185
        $this->doc->setY('-'.($this->doc->get("font_size") + $this->doc->get("font_padding_top")));
186
        $this->doc->addText($apointX["due_date"], $this->doc->get('y'), $this->doc->get("font_size"), "<b>Total:</b>");
187
        $this->doc->addText($apointX["amount"] - $this->doc->getTextWidth($this->doc->get("font_size"), "<b>".number_format($total, 2, ",", ".")."</b>"), $this->doc->get('y'), $this->doc->get("font_size"), "<b>".number_format($total, 2, ",", ".")."</b>");
188
        $this->doc->setY('-'.$this->doc->get("font_padding_bottom"));
189
        $this->doc->line($apointX["due_date"], $this->doc->get('y'), $this->doc->get("right_margin_position"), $this->doc->get('y'));
190
191
        $parameter = array(
192
            "contact" => $reminder->contact,
193
            "payment_text" => "Kontakt ".$reminder->contact->get("number"),
194
            "amount" => $total,
195
            "payment" => $reminder->get('payment_total'),
196
            "due_date" => $reminder->get("dk_due_date"),
197
            "girocode" => $reminder->get("girocode"));
198
199
200
        $this->addPaymentCondition($reminder->get("payment_method_key"), $parameter, $reminder->getPaymentInformation());
201
    }
202
}
203