OutboundDimensions   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 1
dl 71
loc 71
ccs 0
cts 33
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A getWidth() 12 12 3
A getHeight() 12 12 3
A isVertical() 4 4 1
A getRatio() 4 4 1

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
 * HiPanel core package
4
 *
5
 * @link      https://hipanel.com/
6
 * @package   hipanel-core
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\widgets\filePreview;
12
13
/**
14
 * Class OutboundDimensions implements [[DimensionsInterface]] and provides
15
 * API to resize [[originalDimensions]] to [[targetDimensions]] using the following logic:.
16
 *
17
 * The smallest side (width or height) of the [[originalDimensions]] will be resized to
18
 * the [[targetDimensions]] side. The second side will be resized accordingly to the original
19
 * aspect ratio.
20
 *
21
 * Example:
22
 * ```php
23
 * $size = new OutboundDimensions(
24
 *     new Dimensions(500, 1000),
25
 *     new Dimensions(300, 300)
26
 * );
27
 *
28
 * $width = $size->getWidth(); // 300 (matches target width)
29
 * $height = $size->getHeight(); // 600 (resized to keep aspect ratio)
30
 * ```
31
 */
32 View Code Duplication
class OutboundDimensions implements DimensionsInterface
0 ignored issues
show
Duplication introduced by
This class 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...
33
{
34
    /**
35
     * @var Dimensions
36
     */
37
    private $originalDimensions;
38
    /**
39
     * @var Dimensions
40
     */
41
    private $targetDimensions;
42
43
    /**
44
     * OutboundDimensions constructor.
45
     *
46
     * @param Dimensions $originalDimensions the original image dimensions
47
     * @param Dimensions $targetDimensions the target image dimensions
48
     */
49
    public function __construct(Dimensions $originalDimensions, Dimensions $targetDimensions)
50
    {
51
        $this->originalDimensions = $originalDimensions;
52
        $this->targetDimensions = $targetDimensions;
53
    }
54
55
    /**
56
     * @return int
57
     */
58
    public function getWidth()
59
    {
60
        if ($this->originalDimensions->getWidth() < $this->targetDimensions->getWidth()) {
61
            return $this->originalDimensions->getWidth();
62
        }
63
64
        if ($this->isVertical()) {
65
            return $this->targetDimensions->getWidth();
66
        }
67
68
        return $this->targetDimensions->getWidth();
69
    }
70
71
    /**
72
     * @return float|int
73
     */
74
    public function getHeight()
75
    {
76
        if ($this->originalDimensions->getHeight() < $this->targetDimensions->getHeight()) {
77
            return $this->originalDimensions->getHeight();
78
        }
79
80
        if ($this->isVertical()) {
81
            return intval(ceil($this->targetDimensions->getHeight() / $this->originalDimensions->getRatio()));
82
        }
83
84
        return intval(ceil($this->targetDimensions->getHeight() / $this->originalDimensions->getRatio()));
85
    }
86
87
    /**
88
     * @return bool whether image is in the portrait orientation
89
     */
90
    private function isVertical()
91
    {
92
        return $this->originalDimensions->getRatio() < 1;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function getRatio()
99
    {
100
        return $this->originalDimensions->getRatio();
101
    }
102
}
103