Intraface_Pdf   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 238
Duplicated Lines 10.08 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 48.15%

Importance

Changes 0
Metric Value
dl 24
loc 238
rs 10
c 0
b 0
f 0
ccs 52
cts 108
cp 0.4815
wmc 26
lcom 1
cbo 1

11 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 41 1
A calculateDynamicValues() 0 15 1
A setValue() 0 8 2
B setX() 12 12 6
B setY() 12 12 6
A addHeader() 0 23 3
A roundRectangle() 0 13 1
A writeDocument() 0 6 1
A addText() 0 5 1
A nextPage() 0 10 2
A get() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * PdfMaker for Intraface
4
 *
5
 * @author Sune Jensen <[email protected]>
6
 */
7
class Intraface_Pdf extends Intraface_LegacyCpdf
8
{
9
    protected $value;
10
    protected $page;
11
    protected $page_height;
12
    protected $page_width;
13
14 3
    public function __construct()
15
    {
16 3
        $this->page_width = 595;
17 3
        $this->page_height = 841;
18
19
        // Predefined values.
20 3
        $this->value['margin_top'] = 50;
21 3
        $this->value['margin_right'] = 42;
22 3
        $this->value['margin_left'] = 42; // From 0 to the edge in the left side
23 3
        $this->value['margin_bottom'] = 50;
24
25 3
        $this->value['header_height'] = 51;
26 3
        $this->value['header_margin_top'] = 20;
27 3
        $this->value['header_margin_bottom'] = 20;
28
29 3
        $this->value['font_size'] = 11;
30 3
        $this->value['font_padding_top'] = 1;
31 3
        $this->value['font_padding_bottom'] = 4;
32
33 3
        $this->page = 1;
34
35
        // Creates a new A4 document
36 3
        parent::__construct(array(0, 0, $this->page_width, $this->page_height));
37
38
        // Rewrites the placement of Danish characters æ, ø, å, Æ, Ø, Å
39
        // After the Cpdf documentation
40
        // Table for the characters placements can be found here: http://www.fingertipsoft.com/3dkbd/ansitable.html
41
        // Table for their names is found here: http://www.gust.org.pl/fonty/qx-table2.htm
42
        // Notice that the placement of the characters are different in the two tables. Placement is correct in the first.
43
44 3
        $diff = array(230 => 'ae',
45 3
                      198 => 'AE',
46 3
                      248 => 'oslash',
47 3
                      216 => 'Oslash',
48 3
                      229 => 'aring',
49 3
                      197 => 'Aring');
50
51 3
        parent::selectFont('Helvetica.afm', array('differences' => $diff));
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (selectFont() instead of __construct()). Are you sure this is correct? If so, you might want to change this to $this->selectFont().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
Documentation introduced by
array('differences' => $diff) is of type array<string,array<integ...\"197\":\"string\"}>"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
52
53 3
        $this->calculateDynamicValues();
54 3
    }
55
56
    /**
57
     * Calculates all the dynamic values
58
     * Notice that X and Y are reset.
59
     *
60
     * @return void
61
     */
62 3
    private function calculateDynamicValues()
63
    {
64
        // Sets values based on the predefined values.
65 3
        $this->value['right_margin_position'] = $this->page_width - $this->value['margin_right']; // content_width from 0 to right margen
66 3
        $this->value['top_margin_position'] = $this->page_height - $this->value['margin_top']; // content_height
67
68 3
        $this->value['content_width'] = $this->page_width - $this->value['margin_right'] - $this->value['margin_left']; // content_width fom 0 to right margen
69 3
        $this->value['content_height'] = $this->page_height - $this->value['margin_bottom'] - $this->value['margin_top']; // content_height
70
71 3
        $this->value['font_spacing'] = $this->value['font_size'] + $this->value['font_padding_top'] + $this->value['font_padding_bottom'];
72
73
        // X and Y are need to be reset, if the margins are changed.
74 3
        $this->setX(0);
75 3
        $this->setY(0);
76 3
    }
77
78
    /**
79
     * Sets value
80
     *
81
     * @param integer $key   The key to set
82
     * @param integer $value The value to put in the key
83
     *
84
     * @return void
85
     */
86 1
    public function setValue($key, $value)
87
    {
88 1
        $this->value[$key] = $value;
89
        // Every time we change a fixed value we need to update the dynamic values
90 1
        if (in_array($key, array('margin_right', 'margin_left', 'margin_top', 'margin_bottom', 'font_size', 'font_padding_top', 'font_padding_bottom'))) {
91 1
            $this->calculateDynamicValues();
92 1
        }
93 1
    }
94
95
    /**
96
     * Sets the x
97
     *
98
     * @param integer $value The x for the page
99
     *
100
     * @return void
101
     */
102 3 View Code Duplication
    public function setX($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
    {
104 3
        if (is_int($value)) {
105 3
            $this->value['x'] = $this->get('margin_left') + $value;
106 3
        } elseif (is_string($value) && substr($value, 0, 1) == "+") {
107
            $this->value['x'] +=  intval(substr($value, 1));
108
        } elseif (is_string($value) && substr($value, 0, 1) == "-") {
109
            $this->value['x'] -= intval(substr($value, 1));
110
        } else {
111
            throw new Exception('Ugyldig værdi i setX: '.$value);
112
        }
113 3
    }
114
115
    /**
116
     * Sets the y
117
     *
118
     * @param integer $value The y for the page
119
     *
120
     * @return void
121
     */
122 3 View Code Duplication
    public function setY($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
123
    {
124 3
        if (is_int($value)) {
125 3
            $this->value['y'] = $this->page_height - $this->get('margin_top') - $value;
126 3
        } elseif (is_string($value) && substr($value, 0, 1) == "+") {
127
            $this->value['y'] += intval(substr($value, 1));
128
        } elseif (is_string($value) && substr($value, 0, 1) == "-") {
129
            $this->value['y'] -= intval(substr($value, 1));
130
        } else {
131
            throw new Exception("Ugyldig værdi i setY: ".$value);
132
        }
133 3
    }
134
135
    /**
136
     * Adds the header to the document
137
     *
138
     * @param string $headerImg The filepath for the header image
139
     *
140
     * @return void
141
     */
142
    public function addHeader($headerImg = '')
143
    {
144
        if (!file_exists($headerImg)) {
145
            return false;
146
        }
147
148
        $header = parent::openObject();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (openObject() instead of addHeader()). Are you sure this is correct? If so, you might want to change this to $this->openObject().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
149
        $size = getImageSize($headerImg); // array(0 => width, 1 => height)
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
150
151
        $height = $this->get('header_height');
152
        ;
153
        $width = $size[0] * ($height/$size[1]);
154
155
        if ($width > $this->get('content_width')) {
156
            $width = $this->get('content_width');
157
            $height = $size[1] * ($width/$size[0]);
158
        }
159
        parent::addJpegFromFile($headerImg, $this->get('right_margin_position') - $width, $this->page_height - $this->get('header_margin_top') - $height, $width, $height); // , ($this->value["page_width"] - $this->value["margin_left"])/10
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (addJpegFromFile() instead of addHeader()). Are you sure this is correct? If so, you might want to change this to $this->addJpegFromFile().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
Unused Code Comprehensibility introduced by
64% 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...
160
        parent::closeObject();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (closeObject() instead of addHeader()). Are you sure this is correct? If so, you might want to change this to $this->closeObject().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
161
        parent::addObject($header, "all");
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (addObject() instead of addHeader()). Are you sure this is correct? If so, you might want to change this to $this->addObject().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
162
        $this->setValue('margin_top', $height + $this->get('header_margin_top') + $this->get('header_margin_bottom'));
163
        $this->setY(0);
164
    }
165
166
   /**
167
     * create a round rectangle
168
     *
169
     * @param integer $x      The starting x point
170
     * @param integer $y      The starting y point
171
     * @param integer $width  The width of the rectangle
172
     * @param integer $height The height of the rectangle
173
     * @param integer $round  How much to round the rectangle
174
     *
175
     * @return void
176
     */
177
    public function roundRectangle($x, $y, $width, $height, $round)
178
    {
179
        parent::setLineStyle(1);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (setLineStyle() instead of roundRectangle()). Are you sure this is correct? If so, you might want to change this to $this->setLineStyle().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
180
        parent::line($x, $y+$round, $x, $y+$height-$round);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (line() instead of roundRectangle()). Are you sure this is correct? If so, you might want to change this to $this->line().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
181
        parent::line($x+$round, $y+$height, $x+$width-$round, $y+$height);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (line() instead of roundRectangle()). Are you sure this is correct? If so, you might want to change this to $this->line().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
182
        parent::line($x+$width, $y+$height-$round, $x+$width, $y+$round-1);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (line() instead of roundRectangle()). Are you sure this is correct? If so, you might want to change this to $this->line().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
183
        parent::line($x+$width-$round, $y, $x+$round, $y);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (line() instead of roundRectangle()). Are you sure this is correct? If so, you might want to change this to $this->line().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
184
185
        parent::partEllipse($x+$round, $y+$round, 180, 270, $round);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (partEllipse() instead of roundRectangle()). Are you sure this is correct? If so, you might want to change this to $this->partEllipse().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
186
        parent::partEllipse($x+$round, $y+$height-$round, 90, 180, $round);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (partEllipse() instead of roundRectangle()). Are you sure this is correct? If so, you might want to change this to $this->partEllipse().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
187
        parent::partEllipse($x+$width-$round, $y+$height-$round, 0, 90, $round);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (partEllipse() instead of roundRectangle()). Are you sure this is correct? If so, you might want to change this to $this->partEllipse().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
188
        parent::partEllipse($x+$width-$round, $y+$round, 270, 360, $round);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (partEllipse() instead of roundRectangle()). Are you sure this is correct? If so, you might want to change this to $this->partEllipse().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
189
    }
190
191
   /**
192
     * write the document to a file
193
     *
194
     * @param string $data The data to write
195
     *
196
     * @return void
197
     */
198
    public function writeDocument($data, $filnavn)
199
    {
200
        $file = fopen($filnavn, 'wb');
201
        fwrite($file, $data);
202
        fclose($file);
203
    }
204
205
    function addText($x, $y, $size, $text, $angle = 0, $wordSpaceAdjust = 0)
206
    {
207
        $text = utf8_decode($text);
208
        parent::addText($x, $y, $size, $text, $angle = 0, $wordSpaceAdjust = 0);
209
    }
210
211
    /**
212
     * Changes to next page
213
     *
214
     * @param boolean $sub_text What is the sub text
215
     *
216
     * @return integer the new y
217
     */
218
    public function nextPage($sub_text = false)
219
    {
220
        if ($sub_text == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
221
            $this->addText($this->value['right_margin_position'] - $this->getTextWidth($this->value['font_size'], "<i>Fortsættes på næste side...</i>") - 30, $this->value["margin_bottom"] - $this->value['font_padding_top'] - $this->value['font_size'], $this->value['font_size'], "<i>Fortsættes på næste side...</i>");
222
        }
223
        parent::newPage();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (newPage() instead of nextPage()). Are you sure this is correct? If so, you might want to change this to $this->newPage().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
224
        $this->setY(0);
225
        $this->page++;
226
        return $this->get('y');
227
    }
228
229
    /**
230
     * Get values
231
     *
232
     * @param string $key Which string to get
233
     *
234
     * @return mixed
235
     */
236 3
    public function get($key = '')
237
    {
238 3
        if (!empty($key)) {
239 3
            return($this->value[$key]);
240
        } else {
241
            return $this->value;
242
        }
243
    }
244
}
245