ImgHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Kaloa\Renderer\Inigo\Handler;
4
5
use Kaloa\Renderer\Inigo\Handler\ProtoHandler;
6
use Kaloa\Renderer\Inigo\Parser;
7
8
/**
9
 *
10
 */
11
final class ImgHandler extends ProtoHandler
12
{
13
    /**
14
     *
15
     * @var string
16
     */
17
    private $last_img_align = '';
18
19
    /**
20
     *
21
     */
22 18
    public function __construct()
23
    {
24 18
        $this->name = 'img';
25 18
        $this->type = Parser::TAG_OUTLINE;
26
27 18
        $this->defaultParam = 'src';
28 18
    }
29
30
    /**
31
     *
32
     * @param  string $path
33
     * @param  string $alt
34
     * @param  string $title
35
     * @param  int    $align
36
     * @param  string $dir
37
     * @return string
38
     */
39 3
    private function drawImage(
40
        $path,
41
        $alt = '',
42
        $title = '',
0 ignored issues
show
Unused Code introduced by
The parameter $title is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
43
        $align = Parser::PC_IMG_ALIGN_LEFT,
44
        $dir = ''
45
    ) {
46
        // width of framing div's border (one side)
47 3
        $border_width = 1;
48 3
        $padding      = 2;
49 3
        $ret          = '';
50 3
        $path         = $dir . $path;
51
52
        //$title = $this->ParseInline($title);
0 ignored issues
show
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...
53
54 3
        $width = 150;
55 3
        $attr = 'width="150" height="150"';
56
57
        // Image exists?
58 3
        if (@getimagesize($path) !== false) {
59 3
            list($width, $height, $type, $attr) = getimagesize($path);
0 ignored issues
show
Unused Code introduced by
The assignment to $height is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
Unused Code introduced by
The assignment to $type is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
60 3
        }
61
62
        //$path = $this->m_rel_path . $path;
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% 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
64 3
        if ($alt === '') {
65 3
            $alt = $path;
66 3
        }
67
68 3
        $style = 'width: ' . ($width + 2 * (1 + $border_width + $padding)) . 'px;';
69
        //$style2 .= "height: " . ($height + 2 * ($border_width )) . "px;";
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% 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...
70
71 3
        if ($align == Parser::PC_IMG_ALIGN_RIGHT) {
72
            $ret .= '<div class="img">' . "\n"
73 1
                    . '  <div class="img_right" style="' . $style . '">' . "\n"
74 1
                    . '    <div class="img_border">' . "\n"
75 1
                    . '      <img src="' . $this->e($path) . '" alt="Bild: ' . $this->e($alt) . '" title="'
76 1
                        . $this->e($alt) . '" ' . $attr . ' />' . "\n"
77 1
                    . '    </div>' . "\n"
78 1
                    . '    <div class="img_content">';
79 3
        } elseif ($align == Parser::PC_IMG_ALIGN_CENTER) {
80
            $ret .= '<div class="center">' . "\n"
81 1
                    . '  <div class="img">' . "\n"
82 1
                    . '    <div class="img_center" style="' . $style . '">' . "\n"
83 1
                    . '      <div class="img_border">' . "\n"
84 1
                    . '        <img src="' . $this->e($path) . '" alt="Bild: ' . $this->e($alt) . '" title="'
85 1
                        . $this->e($alt) . '" ' . $attr . ' />' . "\n"
86 1
                    . '      </div>' . "\n"
87 1
                    . '      <div class="img_content">';
88 3
        } elseif ($align == Parser::PC_IMG_ALIGN_LEFT) {
89
            $ret .= '<div class="img">' . "\n"
90 3
                    . '  <div class="img_left" style="' . $style . '">' . "\n"
91 3
                    . '    <div class="img_border">' . "\n"
92 3
                    . '      <img src="' . $this->e($path) . '" alt="Bild: ' . $this->e($alt) . '" title="'
93 3
                        . $this->e($alt) . '" ' . $attr . ' />' . "\n"
94 3
                    . '    </div>' . "\n"
95 3
                    . '    <div class="img_content">';
96 3
        }
97
98 3
        return $ret;
99
    }
100
101
    /**
102
     *
103
     * @param  array  $data
104
     * @return string
105
     */
106 3
    public function draw(array $data)
107
    {
108 3
        $ret = '';
0 ignored issues
show
Unused Code introduced by
$ret 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...
109
110 3
        if ($data['front']) {
111 3
            $src   = $this->fillParam($data, 'src', '');
112 3
            $title = $this->fillParam($data, 'title', '');
113 3
            $align = $this->fillParam($data, 'align', null);
114
115
            //$src = $this->m_image_path . $src;
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% 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...
116 3
            $this->last_img_align = $align;
117
118
            switch ($align) {
119 3
                case 'right':
120 1
                    $align = Parser::PC_IMG_ALIGN_RIGHT;
121 1
                    break;
122 3
                case 'center':
123 1
                    $align = Parser::PC_IMG_ALIGN_CENTER;
124 1
                    break;
125 3
                default:
126 3
                    $align = Parser::PC_IMG_ALIGN_LEFT;
127 3
                    break;
128 3
            }
129
130 3
            $ret = $this->drawImage($src, $title, '', $align, $data['vars']['image-dir']);
131 3
        } elseif ($this->last_img_align === 'center') {
132 1
            $this->last_img_align = '';
133 1
            $ret = "</div>\n    </div>\n  </div>\n</div>\n\n";
134 1
        } else {
135 3
            $ret = "</div>\n  </div>\n</div>\n\n";
136
        }
137
138 3
        return $ret;
139
    }
140
}
141