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

nette / schema / 11189863618

05 Oct 2024 02:59AM UTC coverage: 97.384% (+0.7%) from 96.667%
11189863618

push

github

dg
Type::merge() merges arrays only according to the schema (BC break)

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

4 existing lines in 3 files now uncovered.

484 of 497 relevant lines covered (97.38%)

0.97 hits per line

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

97.92
/src/Schema/Expect.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;
11

12
use Nette;
13
use Nette\Schema\Elements\AnyOf;
14
use Nette\Schema\Elements\Structure;
15
use Nette\Schema\Elements\Type;
16

17

18
/**
19
 * Schema generator.
20
 *
21
 * @method static Type scalar($default = null)
22
 * @method static Type string($default = null)
23
 * @method static Type int($default = null)
24
 * @method static Type float($default = null)
25
 * @method static Type bool($default = null)
26
 * @method static Type null()
27
 * @method static Type list($default = [])
28
 * @method static Type mixed($default = null)
29
 * @method static Type email($default = null)
30
 * @method static Type unicode($default = null)
31
 */
32
final class Expect
33
{
34
        public static function __callStatic(string $name, array $args): Type
1✔
35
        {
36
                $type = new Type($name);
1✔
37
                if ($args) {
1✔
38
                        $type->default($args[0]);
1✔
39
                }
40

41
                return $type;
1✔
42
        }
43

44

45
        public static function type(string $type): Type
1✔
46
        {
47
                return new Type($type);
1✔
48
        }
49

50

51
        public static function anyOf(mixed ...$set): AnyOf
1✔
52
        {
53
                return new AnyOf(...$set);
1✔
54
        }
55

56

57
        /**
58
         * @param  Schema[]  $shape
59
         */
60
        public static function structure(array $shape): Structure
1✔
61
        {
62
                return new Structure($shape);
1✔
63
        }
64

65

66
        public static function from(object|string $object, array $items = []): Structure
1✔
67
        {
68
                $ro = new \ReflectionClass($object);
1✔
69
                $props = $ro->hasMethod('__construct')
1✔
70
                        ? $ro->getMethod('__construct')->getParameters()
1✔
71
                        : $ro->getProperties();
1✔
72

73
                foreach ($props as $prop) {
1✔
74
                        \assert($prop instanceof \ReflectionProperty || $prop instanceof \ReflectionParameter);
75
                        if ($item = &$items[$prop->getName()]) {
1✔
76
                                continue;
1✔
77
                        }
78

79
                        $item = new Type($propType = (string) (Nette\Utils\Type::fromReflection($prop) ?? 'mixed'));
1✔
80
                        if (class_exists($propType)) {
1✔
81
                                $item = static::from($propType);
1✔
82
                        }
83

84
                        $hasDefault = match (true) {
1✔
85
                                $prop instanceof \ReflectionParameter => $prop->isOptional(),
1✔
86
                                is_object($object) => $prop->isInitialized($object),
1✔
87
                                default => $prop->hasDefaultValue(),
1✔
88
                        };
89
                        if ($hasDefault) {
1✔
90
                                $default = match (true) {
1✔
91
                                        $prop instanceof \ReflectionParameter => $prop->getDefaultValue(),
1✔
92
                                        is_object($object) => $prop->getValue($object),
1✔
93
                                        default => $prop->getDefaultValue(),
1✔
94
                                };
95
                                if (is_object($default)) {
1✔
96
                                        $item = static::from($default);
1✔
97
                                } else {
98
                                        $item->default($default);
1✔
99
                                }
100
                        } else {
101
                                $item->required();
1✔
102
                        }
103
                }
104

105
                return (new Structure($items))->castTo($ro->getName());
1✔
106
        }
107

108

109
        /**
110
         * @param  Schema[]  $shape
111
         */
112
        public static function array(?array $shape = []): Structure|Type
1✔
113
        {
114
                return $shape
1✔
UNCOV
115
                        ? new Structure($shape)
×
116
                        : (new Type('array'))->default($shape);
1✔
117
        }
118

119

120
        public static function arrayOf(string|Schema $valueType, string|Schema|null $keyType = null): Type
1✔
121
        {
122
                return (new Type('array'))->items($valueType, $keyType);
1✔
123
        }
124

125

126
        public static function listOf(string|Schema $type): Type
1✔
127
        {
128
                return (new Type('list'))->items($type);
1✔
129
        }
130

131

132
        /**
133
         * @param  Schema[]  $shape
134
         */
135
        public static function tuple(array $shape): Structure
1✔
136
        {
137
                if (!array_is_list($shape)) {
1✔
138
                        throw new Nette\InvalidArgumentException('Tuple shape must be indexed array.');
1✔
139
                }
140
                return (new Structure($shape))->castTo('array')->mergeMode(MergeMode::Replace);
1✔
141
        }
142
}
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