• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JBZoo / Data / 7796885268

28 Jan 2024 08:47AM UTC coverage: 98.958%. Remained the same
7796885268

push

github

web-flow
Add PHP 8.3 support and update dependencies in composer.json (#31)

190 of 192 relevant lines covered (98.96%)

38.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/AliasesTrait.php
1
<?php
2

3
/**
4
 * JBZoo Toolbox - Data.
5
 *
6
 * This file is part of the JBZoo Toolbox project.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT
11
 * @copyright  Copyright (C) JBZoo.com, All rights reserved.
12
 * @see        https://github.com/JBZoo/Data
13
 */
14

15
declare(strict_types=1);
16

17
namespace JBZoo\Data;
18

19
use function JBZoo\Utils\bool;
20
use function JBZoo\Utils\float;
21
use function JBZoo\Utils\int;
22

23
/**
24
 * @phan-file-suppress PhanUndeclaredMethod
25
 */
26
trait AliasesTrait
27
{
28
    public function getInt(string $key, int $default = 0): int
29
    {
30
        return int($this->get($key, $default));
12✔
31
    }
32

33
    public function getIntNull(string $key, ?int $default = null): ?int
34
    {
35
        if (!$this->has($key)) {
6✔
36
            return $default;
6✔
37
        }
38

39
        return $this->getInt($key, $default ?? 0);
6✔
40
    }
41

42
    public function getFloat(string $key, float $default = 0.0): float
43
    {
44
        return float($this->get($key, $default));
12✔
45
    }
46

47
    public function getFloatNull(string $key, ?float $default = null): ?float
48
    {
49
        if (!$this->has($key)) {
6✔
50
            return $default;
6✔
51
        }
52

53
        return $this->getFloat($key, $default ?? 0.0);
6✔
54
    }
55

56
    public function getString(string $key, string $default = ''): string
57
    {
58
        return (string)$this->get($key, $default);
12✔
59
    }
60

61
    public function getStringNull(string $key, ?string $default = null): ?string
62
    {
63
        if (!$this->has($key)) {
6✔
64
            return $default;
6✔
65
        }
66

67
        return $this->getString($key, $default ?? '');
6✔
68
    }
69

70
    public function getArray(string $key, array $default = []): array
71
    {
72
        return (array)$this->get($key, $default);
12✔
73
    }
74

75
    public function getArrayNull(string $key, ?array $default = null): ?array
76
    {
77
        if (!$this->has($key)) {
6✔
78
            return $default;
6✔
79
        }
80

81
        return $this->getArray($key, $default ?? []);
6✔
82
    }
83

84
    /**
85
     * @SuppressWarnings(PHPMD.BooleanGetMethodName)
86
     */
87
    public function getBool(string $key, bool $default = false): bool
88
    {
89
        return bool($this->get($key, $default));
12✔
90
    }
91

92
    /**
93
     * @SuppressWarnings(PHPMD.BooleanGetMethodName)
94
     */
95
    public function getBoolNull(string $key, ?bool $default = null): ?bool
96
    {
97
        if (!$this->has($key)) {
6✔
98
            return $default;
6✔
99
        }
100

101
        return $this->getBool($key, $default ?? false);
6✔
102
    }
103

104
    /**
105
     * @psalm-suppress UnsafeInstantiation
106
     * @psalm-suppress ImplicitToStringCast
107
     */
108
    public function getSelf(string $key, array|self $default = []): self
109
    {
110
        if ($this->has($key) && $this->get($key) !== null) {
12✔
111
            // @phpstan-ignore-next-line
112
            return new static((array)$this->get($key, $default));
12✔
113
        }
114

115
        // @phpstan-ignore-next-line
116
        return new static($default);
6✔
117
    }
118

119
    /**
120
     * @psalm-suppress UnsafeInstantiation
121
     * @psalm-suppress ImplicitToStringCast
122
     */
123
    public function getSelfNull(string $key, null|array|self $default = null): ?self
124
    {
125
        if (!$this->has($key)) {
6✔
126
            // @phpstan-ignore-next-line
127
            return $default !== null ? new static($default) : null;
6✔
128
        }
129

130
        return $this->getSelf($key, $default ?? []);
6✔
131
    }
132

133
    public function findInt(string $key, int $default = 0): int
134
    {
135
        return int($this->find($key, $default));
6✔
136
    }
137

138
    public function findFloat(string $key, float $default = 0.0): float
139
    {
140
        return float($this->find($key, $default));
6✔
141
    }
142

143
    public function findString(string $key, string $default = ''): string
144
    {
145
        return (string)$this->find($key, $default);
6✔
146
    }
147

148
    public function findArray(string $key, array $default = []): array
149
    {
150
        return (array)$this->find($key, $default);
6✔
151
    }
152

153
    public function findBool(string $key, bool $default = false): bool
154
    {
155
        return bool($this->find($key, $default));
6✔
156
    }
157

158
    /**
159
     * @psalm-suppress UnsafeInstantiation
160
     */
161
    public function findSelf(string $key, array $default = []): self
162
    {
163
        if ($this->has($key) && $this->get($key) !== null) {
6✔
164
            // @phpstan-ignore-next-line
165
            return new static((array)$this->find($key, $default));
6✔
166
        }
167

168
        // @phpstan-ignore-next-line
169
        return new static($default);
6✔
170
    }
171
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc