FlockAttachmentViewImage   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 95
Duplicated Lines 40 %

Coupling/Cohesion

Components 3
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
lcom 3
cbo 1
dl 38
loc 95
ccs 0
cts 37
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B original() 19 19 6
B thumbnail() 19 19 6
A filename() 0 6 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
namespace NotificationChannels\Flock;
4
5
use NotificationChannels\Flock\Exceptions\CouldNotSendNotification;
6
7
class FlockAttachmentViewImage
8
{
9
    /**
10
     * Image object.
11
     *
12
     * @var array
13
     */
14
    public $original;
15
16
    /**
17
     * Thumbnail object.
18
     *
19
     * @var array
20
     */
21
    public $thumbnail;
22
23
    /**
24
     * Filename for the attached image.
25
     *
26
     * @var string
27
     */
28
    public $filename;
29
30
    /**
31
     * Attach origin al image to attachment.
32
     *
33
     * @param string $src
34
     * @param int|null $height
35
     * @param int|null $width
36
     *
37
     * @return $this
38
     */
39 View Code Duplication
    public function original($src, $height, $width)
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...
40
    {
41
        if (! filter_var($src, FILTER_VALIDATE_URL)) {
42
            throw CouldNotSendNotification::flockAttachmentViewImageException('Source of image in attachment is missing or invalid.');
43
        }
44
        $this->original['src'] = $src;
45
46
        if (! filter_var($height, FILTER_VALIDATE_INT) or $height <= 0) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
47
            throw CouldNotSendNotification::flockAttachmentViewImageException('Height of image in attachment is missing or invalid.');
48
        }
49
        $this->original['height'] = $height;
50
51
        if (! filter_var($width, FILTER_VALIDATE_INT) or $width <= 0) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
52
            throw CouldNotSendNotification::flockAttachmentViewImageException('Width of image in attachment is missing or invalid.');
53
        }
54
        $this->original['width'] = $width;
55
56
        return $this;
57
    }
58
59
    /**
60
     * Attach thumbnail to attachment.
61
     *
62
     * @param string $src
63
     * @param int|null $height
64
     * @param int|null $width
65
     *
66
     * @return $this
67
     */
68 View Code Duplication
    public function thumbnail($src, $height, $width)
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...
69
    {
70
        if (! filter_var($src, FILTER_VALIDATE_URL)) {
71
            throw CouldNotSendNotification::flockAttachmentViewImageException('Source of image in attachment is missing or invalid.');
72
        }
73
        $this->thumbnail['src'] = $src;
74
75
        if (! filter_var($height, FILTER_VALIDATE_INT) or $height <= 0) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
76
            throw CouldNotSendNotification::flockAttachmentViewImageException('Height of image in attachment is missing or invalid.');
77
        }
78
        $this->thumbnail['height'] = $height;
79
80
        if (! filter_var($width, FILTER_VALIDATE_INT) or $width <= 0) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
81
            throw CouldNotSendNotification::flockAttachmentViewImageException('Width of image in attachment is missing or invalid.');
82
        }
83
        $this->thumbnail['width'] = $width;
84
85
        return $this;
86
    }
87
88
    /**
89
     * Add file name to attached image.
90
     *
91
     * @param string $filename
92
     *
93
     * @return $this
94
     */
95
    public function filename($filename)
96
    {
97
        $this->filename = $filename;
98
99
        return $this;
100
    }
101
}
102