Passed
Pull Request — master (#53)
by
unknown
01:19
created

Block   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 78
rs 10
c 1
b 0
f 0
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setType() 0 5 1
A __construct() 0 3 1
A getBlockId() 0 3 1
A removeNulls() 0 8 5
A getType() 0 3 1
A toArray() 0 8 1
A setBlockId() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Nexylan packages.
7
 *
8
 * (c) Nexylan SAS <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Nexy\Slack;
15
16
/**
17
 * @author Sullivan Senechal <[email protected]>
18
 * @author Mikey McLellan <[email protected]>
19
 */
20
class Block
21
{
22
    /**
23
     * @var string
24
     */
25
    protected $type;
26
27
    /**
28
     * @var string
29
     */
30
    private $blockId;
31
32
    public function __construct(?string $type)
33
    {
34
        $this->setType($type);
0 ignored issues
show
Bug introduced by
It seems like $type can also be of type null; however, parameter $type of Nexy\Slack\Block::setType() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
        $this->setType(/** @scrutinizer ignore-type */ $type);
Loading history...
35
    }
36
37
    /**
38
     * @return string
39
     */
40
    public function getType(): string
41
    {
42
        return $this->type;
43
    }
44
45
    /**
46
     * @param string $type
47
     *
48
     * @return $this
49
     */
50
    public function setType(string $type): self
51
    {
52
        $this->type = $type;
53
54
        return $this;
55
    }
56
57
    /**
58
     * Convert this block to its array representation.
59
     *
60
     * @return array
61
     */
62
    public function toArray(): array
63
    {
64
        $data = [
65
            'type' => $this->type,
66
            'block_id' => $this->blockId
67
        ];
68
69
        return $this->removeNulls($data);
70
    }
71
72
    /**
73
     * @param string $blockId
74
     */
75
    public function setBlockId(string $blockId): self
76
    {
77
        $this->blockId = $blockId;
78
79
        return $this;
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    public function getBlockId(): string
86
    {
87
        return $this->blockId;
88
    }
89
90
    protected function removeNulls(array $data): array
91
    {
92
        foreach($data as $k => $v) {
93
            if ($v === null || (is_array($v) && count($v) === 0)) {
94
                unset($data[$k]);
95
            }
96
        }
97
        return $data;
98
    }
99
}
100