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

nette / schema / 6424286167

05 Oct 2023 08:44PM UTC coverage: 96.567%. Remained the same
6424286167

push

github

dg
increase phpstan level to 8 [Closes #54]

1 of 1 new or added line in 1 file covered. (100.0%)

450 of 466 relevant lines covered (96.57%)

0.97 hits per line

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

87.93
/src/Schema/Elements/Base.php
1
<?php
2

3
/**
4
 * This file is part of the Nette Framework (https://nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
declare(strict_types=1);
9

10
namespace Nette\Schema\Elements;
11

12
use Nette;
13
use Nette\Schema\Context;
14
use Nette\Schema\Helpers;
15

16

17
/**
18
 * @internal
19
 */
20
trait Base
21
{
22
        private bool $required = false;
23
        private mixed $default = null;
24

25
        /** @var ?callable */
26
        private $before;
27

28
        /** @var callable[] */
29
        private array $transforms = [];
30
        private ?string $deprecated = null;
31

32

33
        public function default(mixed $value): self
1✔
34
        {
35
                $this->default = $value;
1✔
36
                return $this;
1✔
37
        }
38

39

40
        public function required(bool $state = true): self
1✔
41
        {
42
                $this->required = $state;
1✔
43
                return $this;
1✔
44
        }
45

46

47
        public function before(callable $handler): self
1✔
48
        {
49
                $this->before = $handler;
1✔
50
                return $this;
1✔
51
        }
52

53

54
        public function castTo(string $type): self
1✔
55
        {
56
                return $this->transform(Helpers::getCastStrategy($type));
1✔
57
        }
58

59

60
        public function transform(callable $handler): self
1✔
61
        {
62
                $this->transforms[] = $handler;
1✔
63
                return $this;
1✔
64
        }
65

66

67
        public function assert(callable $handler, ?string $description = null): self
1✔
68
        {
69
                $expected = $description ?: (is_string($handler) ? "$handler()" : '#' . count($this->transforms));
1✔
70
                return $this->transform(function ($value, Context $context) use ($handler, $description, $expected) {
1✔
71
                        if ($handler($value)) {
1✔
72
                                return $value;
1✔
73
                        }
74
                        $context->addError(
1✔
75
                                'Failed assertion ' . ($description ? "'%assertion%'" : '%assertion%') . ' for %label% %path% with value %value%.',
1✔
76
                                Nette\Schema\Message::FailedAssertion,
1✔
77
                                ['value' => $value, 'assertion' => $expected],
1✔
78
                        );
79
                });
1✔
80
        }
81

82

83
        /** Marks as deprecated */
84
        public function deprecated(string $message = 'The item %path% is deprecated.'): self
1✔
85
        {
86
                $this->deprecated = $message;
1✔
87
                return $this;
1✔
88
        }
89

90

91
        public function completeDefault(Context $context): mixed
1✔
92
        {
93
                if ($this->required) {
1✔
94
                        $context->addError(
1✔
95
                                'The mandatory item %path% is missing.',
1✔
96
                                Nette\Schema\Message::MissingItem,
1✔
97
                        );
98
                        return null;
1✔
99
                }
100

101
                return $this->default;
1✔
102
        }
103

104

105
        public function doNormalize(mixed $value, Context $context): mixed
1✔
106
        {
107
                if ($this->before) {
1✔
108
                        $value = ($this->before)($value);
1✔
109
                }
110

111
                return $value;
1✔
112
        }
113

114

115
        private function doDeprecation(Context $context): void
1✔
116
        {
117
                if ($this->deprecated !== null) {
1✔
118
                        $context->addWarning(
1✔
119
                                $this->deprecated,
1✔
120
                                Nette\Schema\Message::Deprecated,
1✔
121
                        );
122
                }
123
        }
1✔
124

125

126
        private function doTransform(mixed $value, Context $context): mixed
1✔
127
        {
128
                $isOk = $context->createChecker();
1✔
129
                foreach ($this->transforms as $handler) {
1✔
130
                        $value = $handler($value, $context);
1✔
131
                        if (!$isOk()) {
1✔
132
                                return null;
1✔
133
                        }
134
                }
135
                return $value;
1✔
136
        }
137

138

139
        /** @deprecated use Nette\Schema\Validators::validateType() */
140
        private function doValidate(mixed $value, string $expected, Context $context): bool
141
        {
142
                $isOk = $context->createChecker();
×
143
                Helpers::validateType($value, $expected, $context);
×
144
                return $isOk();
×
145
        }
146

147

148
        /** @deprecated use Nette\Schema\Validators::validateRange() */
149
        private static function doValidateRange(mixed $value, array $range, Context $context, string $types = ''): bool
150
        {
151
                $isOk = $context->createChecker();
×
152
                Helpers::validateRange($value, $range, $context, $types);
×
153
                return $isOk();
×
154
        }
155

156

157
        /** @deprecated use doTransform() */
158
        private function doFinalize(mixed $value, Context $context): mixed
159
        {
160
                return $this->doTransform($value, $context);
×
161
        }
162
}
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

© 2026 Coveralls, Inc