RectangularFromPointWidthHeight::__construct()   B
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 8.8571
cc 5
eloc 7
nc 2
nop 4
1
<?php
2
3
/*
4
 * This file is part of the CGI-Calc package.
5
 *
6
 * (c) Milos Tomic <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Cgi\Calc\Field;
13
14
use Cgi\Calc\Point;
15
use Cgi\Calc\Point\PointSet;
16
17
class RectangularFromPointWidthHeight extends AbstractFieldProducer
18
{
19
    /** @var Point */
20
    private $lowerLeft;
21
22
    /** @var int */
23
    private $width;
24
25
    /** @var int */
26
    private $height;
27
28
    /**
29
     * @param Point                  $lowerLeft
30
     * @param int                    $width
31
     * @param int                    $height
32
     * @param ValueProvider|callable $valueProvider
33
     */
34
    public function __construct(Point $lowerLeft, $width, $height, $valueProvider = null)
35
    {
36
        parent::__construct($valueProvider);
37
38
        if (false === is_int($width) || false === is_int($height) || $width < 0 || $height < 0) {
39
            throw new \InvalidArgumentException('Width and height must be integers');
40
        }
41
42
        $this->lowerLeft = $lowerLeft;
43
        $this->width = $width;
44
        $this->height = $height;
45
    }
46
47
    /**
48
     * @return PointSet
49
     */
50 View Code Duplication
    public function produce()
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...
51
    {
52
        $result = new PointSet();
53
        foreach ($this->lowerLeft->forXTimes($this->width) as $x) {
54
            foreach ($this->lowerLeft->forYTimes($this->height) as $y) {
55
                $point = new Point($x, $y);
56
                $value = $this->getValue($point);
57
                $result->attach($point, $value);
58
            }
59
        }
60
61
        return $result;
62
    }
63
}
64