Completed
Push — master ( 30f8c9...32b148 )
by Edgar
05:23 queued 01:57
created

Rect::getCenterX()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
namespace nstdio\svg\shape;
3
4
use nstdio\svg\ElementInterface;
5
6
/**
7
 * Class Rect
8
 * The 'rect' element defines a rectangle which is axis-aligned with the current user coordinate system. Rounded
9
 * rectangles can be achieved by setting appropriate values for attributes 'rx' and 'ry'.
10
 *
11
 * @link    https://www.w3.org/TR/SVG11/shapes.html#RectElement
12
 * @property float rx For rounded rectangles, the x-axis radius of the ellipse used to round off the corners of the
13
 *           rectangle. A negative value is an error
14
 * @property float ry For rounded rectangles, the y-axis radius of the ellipse used to round off the corners of the
15
 *           rectangle. A negative value is an error
16
 * @property float x  The x-axis coordinate of the side of the rectangle which has the smaller x-axis coordinate value
17
 *           in the current user coordinate system. If the attribute is not specified, the effect is as if a value of
18
 *           "0" were specified.
19
 * @property float y  The y-axis coordinate of the side of the rectangle which has the smaller y-axis coordinate value
20
 *           in the current user coordinate system. If the attribute is not specified, the effect is as if a value of
21
 *           "0" were specified.
22
 * @package nstdio\svg\shape
23
 * @author  Edgar Asatryan <[email protected]>
24
 */
25
class Rect extends Shape
26
{
27
    public function __construct(ElementInterface $parent, $height, $width, $x = 0, $y = 0)
28
    {
29
        parent::__construct($parent);
30
31
        $this->height = $height;
32
        $this->width = $width;
33
        $this->x = $x;
34
        $this->y = $y;
35
    }
36
37
    /**
38
     * @param float $radius
39
     */
40
    public function setBorderRadius($radius)
41
    {
42
        $this->rx = $radius;
43
        $this->ry = $radius;
44
    }
45
46
    /**
47
     * @return Path
48
     */
49
    public function toPath()
50
    {
51
        $path = new Path($this->getRoot(), $this->x + $this->rx, $this->y);
0 ignored issues
show
Documentation introduced by
$this->getRoot() is of type object<nstdio\svg\XMLDocumentInterface>, but the function expects a object<nstdio\svg\ElementInterface>.

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
        list($x, $y, $width, $height, $rx, $ry) = [$this->x, $this->y, $this->width, $this->height, $this->rx, $this->ry];
53
        if ($rx === null) {
54
            $rx = 0;
55
        }
56
        if ($ry === null) {
57
            $ry = 0;
58
        }
59
        $path->hLineTo($x + $width - $rx)
60
            ->arcTo($rx, $ry, 0, false, true, $x + $width, $y + $ry)
61
            ->lineTo($x + $width, $y + $height - $ry)
62
            ->arcTo($rx, $ry, 0, false, true, $x + $width - $rx, $y + $height)
63
            ->lineTo($x + $rx, $y + $height)
64
            ->arcTo($rx, $ry, 0, false, true, $x, $y + $height - $ry)
65
            ->lineTo($x, $y + $ry)
66
            ->arcTo($rx, $ry, 0, false, true, $x + $rx, $y);
67
68
        $attributes = $this->allAttributes(['width', 'height', 'x', 'y', 'rx', 'ry']);
69
70
        foreach ($attributes as $key => $value) {
71
            $path->{$key} = $value;
72
        }
73
74
        return $path;
75
    }
76
77
    public function getName()
78
    {
79
        return 'rect';
80
    }
81
82
    public function getCenterX()
83
    {
84
        return $this->x + ($this->width / 2);
85
    }
86
87
    protected function getCenterY()
88
    {
89
        return $this->y + ($this->width / 2);
90
    }
91
}