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

quadraticBezierBox.php ➔ drawQuad()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 25
c 1
b 0
f 1
nc 1
nop 6
dl 0
loc 39
rs 8.8571
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 18 and the first side effect is on line 10.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
use nstdio\svg\container\G;
3
use nstdio\svg\container\SVG;
4
use nstdio\svg\shape\Circle;
5
use nstdio\svg\shape\Path;
6
use nstdio\svg\shape\Rect;
7
use nstdio\svg\text\Text;
8
use nstdio\svg\util\Bezier;
9
10
require_once __DIR__ . '/../vendor/autoload.php';
11
12
for ($i = 0; $i < 2; $i++) {
13
    drawQuad(mt_rand(0, 980), mt_rand(0, 480), mt_rand(0, 980), mt_rand(0, 480), mt_rand(0, 980), mt_rand(0, 480));
14
}
15
16
drawQuad(10, 5, 10, 150, 50, 80);
17
18
function drawQuad($mx, $my, $x1, $y1, $x, $y)
19
{
20
    $ptRadius = 6;
21
    $svg = new SVG('10cm', '6cm');
22
    $svg->getElement()->setAttribute('viewBox', '0 0 1000 600');
23
    $lineStyle = ['fill' => 'none', 'stroke' => '#888888', 'stroke-width' => 2];
24
25
26
    (new Rect($svg, 498, 998, 1, 1))->apply(['fill' => 'none', 'stroke' => 'blue', 'stroke-width' => '1']);
27
    (new Path($svg, $mx, $my))
28
        ->quadraticCurveTo($x1, $y1, $x, $y)
29
        ->apply(['fill' => 'none', 'stroke' => 'red', 'stroke-width' => 1]);
30
31
    $controlsGroup = new G($svg);
32
    $controlsGroup->fill = 'black';
33
34
    (new Circle($controlsGroup, $mx, $my, $ptRadius));
35
    (new Circle($controlsGroup, $x, $y, $ptRadius));
36
37
38
    $g = (new G($svg))->apply(['fill' => '#888888']);
39
40
    (new Circle($g, $x1, $y1, $ptRadius));
41
42
    $path2 = new Path($svg, $mx, $my);
43
    $path2->lineTo($x1, $y1)
44
        ->lineTo($x, $y)
45
        ->apply($lineStyle)->apply(['stroke-opacity' => 0.5]);
46
47
    $box = Bezier::quadraticBBox($mx, $my, $x1, $y1, $x, $y);
48
49
    $boxRect = (new Rect($svg, $box['height'], $box['width'], $box['x'], $box['y']))->apply($lineStyle)
50
        ->apply(['stroke-dasharray' => '8 8', 'stroke-linecap' => 'round', 'fill' => 'blue', 'fill-opacity' => 0.1]);
51
52
    $coordinates = sprintf("P0(%d, %d), P1(%d, %d), P2(%d, %d). BBox(width: %.2f, height: %.2f, x: %.2f, y: %.2f)", $mx, $my, $x1, $y1, $x, $y, $boxRect->width, $boxRect->height, $boxRect->x, $boxRect->y);
0 ignored issues
show
Bug introduced by
Accessing width on the interface nstdio\svg\ElementInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
Accessing height on the interface nstdio\svg\ElementInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
Accessing x on the interface nstdio\svg\ElementInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
Accessing y on the interface nstdio\svg\ElementInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
53
    (new Text($svg, $coordinates))->apply(['x' => 5, 'y' => 530, 'font-size' => '24px']);
54
55
    echo $svg;
56
}
57