Passed
Push — master ( a82c2e...a14f67 )
by SignpostMarv
12:12
created

NudgePropertyWithUniqueTrimmedStringsOrThings()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 41
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.0327

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 2
nop 4
dl 0
loc 41
ccs 11
cts 13
cp 0.8462
crap 3.0327
rs 9.7666
c 0
b 0
f 0
1
<?php
2
/**
3
* @author SignpostMarv
4
*/
5
declare(strict_types=1);
6
7
namespace SignpostMarv\DaftObject\SchemaOrg;
8
9
use InvalidArgumentException;
10
use SignpostMarv\DaftObject\AbstractArrayBackedDaftObject;
11
use SignpostMarv\DaftObject\DaftJson;
12
use SignpostMarv\DaftObject\DaftObjectHasPropertiesWithMultiTypedArraysOfUniqueValues;
13
use SignpostMarv\DaftObject\JsonTypeUtilities;
14
use SignpostMarv\DaftObject\SchemaOrg\CreativeWork\MediaObject\ImageObject;
15
use SignpostMarv\DaftObject\SchemaOrg\DataTypes\Date;
16
use SignpostMarv\DaftObject\SchemaOrg\DataTypes\DateTime;
17
use SignpostMarv\DaftObject\SchemaOrg\Intangible\StructuredValue\ContactPoint;
18
use SignpostMarv\DaftObject\SchemaOrg\Intangible\StructuredValue\PropertyValue;
19
20
/**
21
* @property array<int, string> $additionalType
22
* @property array<int, string> $alternateName
23
* @property array<int, string> $description
24
* @property array<int, string> $disambiguatingDescription
25
* @property array<int, string|PropertyValue> $identifier
26
* @property array<int, string|ImageObject> $image
27
* @property array<int, string|CreativeWork> $mainEntityOfPage
28
* @property array<int, string> $name
29
* @property array<int, Action> $potentialAction
30
* @property array<int, string> $sameAs
31
* @property array<int, CreativeWork|Event> $subjectOf
32
* @property array<int, Action> $potentialAction
33
* @property array<int, string> $url
34
*
35
* @template-implements DaftJson<Thing>
36
*/
37
class Thing extends AbstractArrayBackedDaftObject implements
38
    DaftJson,
39
    DaftObjectHasPropertiesWithMultiTypedArraysOfUniqueValues
40
{
41
    const SCHEMA_ORG_CONTEXT = 'http://schema.org';
42
43
    const SCHEMA_ORG_TYPE = 'Thing';
44
45
    const PROPERTIES = [
46
        '@context',
47
        '@type',
48
        'additionalType',
49
        'alternateName',
50
        'description',
51
        'disambiguatingDescription',
52
        'identifier',
53
        'image',
54
        'mainEntityOfPage',
55
        'name',
56
        'potentialAction',
57
        'sameAs',
58
        'subjectOf',
59
        'url',
60
    ];
61
62
    const PROPERTIES_WITH_MULTI_TYPED_ARRAYS = [
63
        'additionalType' => [
64
            'string',
65
        ],
66
        'alternateName' => [
67
            'string',
68
        ],
69
        'description' => [
70
            'string',
71
        ],
72
        'disambiguatingDescription' => [
73
            'string',
74
        ],
75
        'identifier' => [
76
            'string',
77
            PropertyValue::class,
78
        ],
79
        'image' => [
80
            'string',
81
            ImageObject::class,
82
        ],
83
        'mainEntityOfPage' => [
84
            'string',
85
            CreativeWork::class,
86
        ],
87
        'name' => [
88
            'string',
89
        ],
90
        'potentialAction' => [
91
            Action::class,
92
        ],
93
        'sameAs' => [
94
            'string',
95
        ],
96
        'subjectOf' => [
97
            CreativeWork::class,
98
            Event::class,
99
        ],
100
        'url' => [
101
            'string',
102
        ],
103
    ];
104
105 9731
    public function __construct(array $data = [], bool $writeAll = false)
106
    {
107 9731
        $missing = array_diff(static::DaftObjectProperties(), array_keys($data));
108
109
        /**
110
        * @var array<int, string>
111
        */
112 9731
        $missing = array_combine(
113 9731
            $missing,
114 9731
            array_fill(0, count($missing), [])
115
        );
116
117
        /**
118
        * @var array<string, scalar|array|object|null>
119
        */
120 9731
        $data = array_merge(
121 9731
            $data,
122 9731
            $missing
123
        );
124
125 9731
        unset($data['@context'], $data['@type']);
126
127 9731
        parent::__construct($data, $writeAll);
128 9731
    }
129
130 240
    public function ObtainContext() : string
131
    {
132 240
        return (string) static::SCHEMA_ORG_CONTEXT;
133
    }
134
135 240
    public function ObtainType() : string
136
    {
137 240
        return (string) static::SCHEMA_ORG_TYPE;
138
    }
139
140
    /**
141
    * @return array<int, string>
142
    */
143 478
    public function GetAdditionalType() : array
144
    {
145
        /**
146
        * @var array<int, string>
147
        */
148 478
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
149 478
            'additionalType',
150 478
            $this->RetrievePropertyValueFromData('additionalType'),
151 478
            static::class
152
        );
153
154 478
        return $out;
155
    }
156
157
    /**
158
    * @param array<int, string> $value
159
    */
160 239
    public function SetAdditionalType(array $value) : void
161
    {
162 239
        $this->NudgePropertyValue(
163 239
            'additionalType',
164 239
            $value,
165 239
            true
166
        );
167 239
    }
168
169
    /**
170
    * @return array<int, string>
171
    */
172 478
    public function GetAlternateName() : array
173
    {
174
        /**
175
        * @var array<int, string>
176
        */
177 478
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
178 478
            'alternateName',
179 478
            $this->RetrievePropertyValueFromData('alternateName'),
180 478
            static::class
181
        );
182
183 478
        return $out;
184
    }
185
186
    /**
187
    * @param array<int, string> $value
188
    */
189 239
    public function SetAlternateName(array $value) : void
190
    {
191 239
        $this->NudgePropertyValue(
192 239
            'alternateName',
193 239
            $value,
194 239
            true
195
        );
196 239
    }
197
198
    /**
199
    * @return array<int, string>
200
    */
201 478
    public function GetDescription() : array
202
    {
203
        /**
204
        * @var array<int, string>
205
        */
206 478
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
207 478
            'description',
208 478
            $this->RetrievePropertyValueFromData('description'),
209 478
            static::class
210
        );
211
212 478
        return $out;
213
    }
214
215
    /**
216
    * @param array<int, string> $value
217
    */
218 239
    public function SetDescription(array $value) : void
219
    {
220 239
        $this->NudgePropertyValue(
221 239
            'description',
222 239
            $value,
223 239
            true
224
        );
225 239
    }
226
227
    /**
228
    * @return array<int, string>
229
    */
230 478
    public function GetDisambiguatingDescription() : array
231
    {
232
        /**
233
        * @var array<int, string>
234
        */
235 478
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
236 478
            'disambiguatingDescription',
237 478
            $this->RetrievePropertyValueFromData('disambiguatingDescription'),
238 478
            static::class
239
        );
240
241 478
        return $out;
242
    }
243
244
    /**
245
    * @param array<int, string> $value
246
    */
247 239
    public function SetDisambiguatingDescription(array $value) : void
248
    {
249 239
        $this->NudgePropertyValue(
250 239
            'disambiguatingDescription',
251 239
            $value,
252 239
            true
253
        );
254 239
    }
255
256
    /**
257
    * @return array<int, string|PropertyValue>
258
    */
259 479
    public function GetIdentifier() : array
260
    {
261
        /**
262
        * @var array<int, string|PropertyValue>
263
        */
264 479
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
265 479
            'identifier',
266 479
            $this->RetrievePropertyValueFromData('identifier'),
267 479
            static::class
268
        );
269
270 479
        return $out;
271
    }
272
273
    /**
274
    * @param array<int, string|PropertyValue> $value
275
    */
276 240
    public function SetIdentifier(array $value) : void
277
    {
278 240
        $this->NudgePropertyValue(
279 240
            'identifier',
280 240
            $value,
281 240
            true
282
        );
283 240
    }
284
285
    /**
286
    * @return array<int, string|ImageObject>
287
    */
288 478
    public function GetImage() : array
289
    {
290
        /**
291
        * @var array<int, string|ImageObject>
292
        */
293 478
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
294 478
            'image',
295 478
            $this->RetrievePropertyValueFromData('image'),
296 478
            static::class
297
        );
298
299 478
        return $out;
300
    }
301
302
    /**
303
    * @param array<int, string|ImageObject> $value
304
    */
305 239
    public function SetImage(array $value) : void
306
    {
307 239
        $this->NudgePropertyValue(
308 239
            'image',
309 239
            $value,
310 239
            true
311
        );
312 239
    }
313
314
    /**
315
    * @return array<int, string|CreativeWork>
316
    */
317 478
    public function GetMainEntityOfPage() : array
318
    {
319
        /**
320
        * @var array<int, string|CreativeWork>
321
        */
322 478
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
323 478
            'mainEntityOfPage',
324 478
            $this->RetrievePropertyValueFromData('mainEntityOfPage'),
325 478
            static::class
326
        );
327
328 478
        return $out;
329
    }
330
331
    /**
332
    * @param array<int, string|CreativeWork> $value
333
    */
334 239
    public function SetMainEntityOfPage(array $value) : void
335
    {
336 239
        $this->NudgePropertyValue(
337 239
            'mainEntityOfPage',
338 239
            $value,
339 239
            true
340
        );
341 239
    }
342
343
    /**
344
    * @return array<int, string>
345
    */
346 478
    public function GetName() : array
347
    {
348
        /**
349
        * @var array<int, string>
350
        */
351 478
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
352 478
            'name',
353 478
            $this->RetrievePropertyValueFromData('name'),
354 478
            static::class
355
        );
356
357 478
        return $out;
358
    }
359
360
    /**
361
    * @param array<int, string> $value
362
    */
363 239
    public function SetName(array $value) : void
364
    {
365 239
        $this->NudgePropertyWithUniqueTrimmedStringsMightNotBeString('name', __METHOD__, $value);
366 239
    }
367
368
    /**
369
    * @return array<int, Action>
370
    */
371 478
    public function GetPotentialAction() : array
372
    {
373
        /**
374
        * @var array<int, Action>
375
        */
376 478
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
377 478
            'potentialAction',
378 478
            $this->RetrievePropertyValueFromData('potentialAction'),
379 478
            static::class
380
        );
381
382 478
        return $out;
383
    }
384
385
    /**
386
    * @param array<int, Action> $value
387
    */
388 239
    public function SetPotentialAction(array $value) : void
389
    {
390 239
        $this->NudgePropertyWithUniqueValuesOfThings(
391 239
            'potentialAction',
392 239
            __METHOD__,
393 239
            $value,
394 239
            Action::class
395
        );
396 239
    }
397
398
    /**
399
    * @return array<int, string>
400
    */
401 478
    public function GetSameAs() : array
402
    {
403
        /**
404
        * @var array<int, string>
405
        */
406 478
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
407 478
            'sameAs',
408 478
            $this->RetrievePropertyValueFromData('sameAs'),
409 478
            static::class
410
        );
411
412 478
        return $out;
413
    }
414
415
    /**
416
    * @param array<int, string> $value
417
    */
418 239
    public function SetSameAs(array $value) : void
419
    {
420 239
        $this->NudgePropertyWithUniqueTrimmedStringsMightNotBeString('sameAs', __METHOD__, $value);
421 239
    }
422
423
    /**
424
    * @return array<int, CreativeWork|Event>
425
    */
426 478
    public function GetSubjectOf() : array
427
    {
428
        /**
429
        * @var array<int, CreativeWork|Event>
430
        */
431 478
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
432 478
            'subjectOf',
433 478
            $this->RetrievePropertyValueFromData('subjectOf'),
434 478
            static::class
435
        );
436
437 478
        return $out;
438
    }
439
440
    /**
441
    * @param array<int, CreativeWork|Event> $value
442
    */
443 239
    public function SetSubjectOf(array $value) : void
444
    {
445 239
        $this->NudgePropertyValue('subjectOf', $value, true);
446 239
    }
447
448
    /**
449
    * @return array<int, string>
450
    */
451 478
    public function GetUrl() : array
452
    {
453
        /**
454
        * @var array<int, string>
455
        */
456 478
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
457 478
            'url',
458 478
            $this->RetrievePropertyValueFromData('url'),
459 478
            static::class
460
        );
461
462 478
        return $out;
463
    }
464
465
    /**
466
    * @param array<int, string> $value
467
    */
468 239
    public function SetUrl(array $value) : void
469
    {
470 239
        $this->NudgePropertyWithUniqueTrimmedStringsMightNotBeString('url', __METHOD__, $value);
471 239
    }
472
473 240
    public function jsonSerialize() : array
474
    {
475 240
        return array_filter(
476 240
            parent::jsonSerialize(),
477
            /**
478
            * @param scalar|array|object|null $val
479
            */
480
            function ($val) : bool {
481 240
                return ! is_array($val) || count($val) > 0;
482 240
            }
483
        );
484
    }
485
486 20304
    public static function DaftObjectProperties() : array
487
    {
488
        /**
489
        * @var array<int, string>
490
        */
491 20304
        $static = static::PROPERTIES;
492
493
        /**
494
        * @var string
495
        *
496
        * @psalm-var class-string<Thing>
497
        */
498 20304
        $static_parent = get_parent_class(static::class);
499
500 20304
        $parent = $static_parent::DaftObjectProperties();
501
502 20304
        return array_unique(array_merge(
503 20304
            $parent,
504 20304
            $static
505
        ));
506
    }
507
508
    /**
509
    * @return array<string, array<int, string>>
510
    */
511 479
    public static function DaftObjectPropertiesWithMultiTypedArraysOfUniqueValues() : array
512
    {
513
        /**
514
        * @var array<string, array<int, string>>
515
        */
516 479
        $static = static::PROPERTIES_WITH_MULTI_TYPED_ARRAYS;
517
518
        /**
519
        * @var string
520
        *
521
        * @psalm-var class-string<Thing>
522
        */
523 479
        $static_parent = get_parent_class(static::class);
524
525 479
        if ($static_parent === get_parent_class(self::class)) {
526 479
            return $static;
527
        }
528
529 478
        $parent = $static_parent::DaftObjectPropertiesWithMultiTypedArraysOfUniqueValues();
530
531 478
        return array_merge(
532 478
            $parent,
533 478
            $static
534
        );
535
    }
536
537 9842
    public static function DaftObjectNullableProperties() : array
538
    {
539
        /**
540
        * @var array<int, string>
541
        */
542 9842
        $static = static::NULLABLE_PROPERTIES;
543
544
        /**
545
        * @var string
546
        *
547
        * @psalm-var class-string<Thing>
548
        */
549 9842
        $static_parent = get_parent_class(static::class);
550
551 9842
        $parent = $static_parent::DaftObjectNullableProperties();
552
553 9842
        return array_unique(array_merge(
554 9842
            $parent,
555 9842
            $static
556
        ));
557
    }
558
559 461
    public static function DaftObjectExportableProperties() : array
560
    {
561 461
        return static::DaftObjectProperties();
562
    }
563
564 479
    public static function DaftObjectJsonProperties() : array
565
    {
566 479
        return static::DaftObjectProperties();
567
    }
568
569 479
    public static function DaftObjectPublicGetters() : array
570
    {
571
        /**
572
        * @var string
573
        *
574
        * @psalm-var class-string<Thing>
575
        */
576 479
        $static_parent = get_parent_class(static::class);
577
578 479
        $static = TypeUtilities::DaftObjectPublicGetters(static::class);
579
580 479
        if ($static_parent === get_parent_class(self::class)) {
581 92
            return $static;
582
        }
583
584 478
        return array_unique(array_merge(
585 478
            TypeUtilities::DaftObjectPublicGetters($static_parent),
586 478
            $static
587
        ));
588
    }
589
590
    public static function DaftObjectPublicOrProtectedGetters() : array
591
    {
592
        /**
593
        * @var string
594
        *
595
        * @psalm-var class-string<Thing>
596
        */
597
        $static_parent = get_parent_class(static::class);
598
599
        $static = TypeUtilities::DaftObjectPublicOrProtectedGetters(static::class);
600
601
        if ($static_parent === get_parent_class(self::class)) {
602
            return $static;
603
        }
604
605
        return array_unique(array_merge(
606
            TypeUtilities::DaftObjectPublicOrProtectedGetters($static_parent),
607
            $static
608
        ));
609
    }
610
611 240
    public static function DaftObjectPublicSetters() : array
612
    {
613
        /**
614
        * @var string
615
        *
616
        * @psalm-var class-string<Thing>
617
        */
618 240
        $static_parent = get_parent_class(static::class);
619
620 240
        $static = TypeUtilities::DaftObjectPublicSetters(static::class);
621
622 240
        if ($static_parent === get_parent_class(self::class)) {
623 2
            return $static;
624
        }
625
626 239
        return array_unique(array_merge(
627 239
            TypeUtilities::DaftObjectPublicSetters($static_parent),
628 239
            $static
629
        ));
630
    }
631
632
    /**
633
    * @return static
634
    *
635
    * @psalm-return Thing
636
    */
637 240
    public static function DaftObjectFromJsonString(
638
        string $string,
639
        bool $writeAll = self::BOOL_DEFAULT_WRITEALL
640
    ) : DaftJson {
641
        /**
642
        * @var array<string, scalar|(scalar|array|object|null)[]|object|null>
643
        */
644 240
        $decoded = json_decode($string, true);
645
646
        /**
647
        * @var static
648
        */
649 240
        $out = static::DaftObjectFromJsonArray($decoded, $writeAll);
650
651 240
        return $out;
652
    }
653
654
    /**
655
    * @return static
656
    *
657
    * @psalm-return Thing
658
    */
659 240
    public static function DaftObjectFromJsonArray(
660
        array $array,
661
        bool $writeAll = self::BOOL_DEFAULT_WRITEALL
662
    ) : DaftJson {
663 240
        $type = JsonTypeUtilities::ThrowIfNotDaftJson(static::class);
664
665 240
        $multi_type = static::DaftObjectPropertiesWithMultiTypedArraysOfUniqueValues();
666
667 240
        $data = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
668
669 240
        $array_keys = array_keys($array);
670
671 240
        $data = array_combine($array_keys, array_map(
672
            /**
673
            * @param int|string $k
674
            *
675
            * @return mixed
676
            */
677
            function ($k) use ($array, $multi_type) {
678 240
                if ( ! is_string($k)) {
679
                    throw new InvalidArgumentException(
680
                        'Argument 1 passed to ' .
681
                        __METHOD__ .
682
                        '() must have all-string indices!'
683
                    );
684
                }
685
686 240
                if (is_array($array[$k])) {
687 240
                    return array_map(
688
                        /**
689
                        * @param mixed $val
690
                        *
691
                        * @return mixed
692
                        */
693
                        function ($val) use ($k, $multi_type) {
694
                            if (
695 240
                                is_array($val) &&
696 240
                                isset($val['@context'], $val['@type'], $multi_type[$k])
697
                            ) {
698 240
                                foreach ($multi_type[$k] as $maybe) {
699
                                    if (
700 240
                                        is_a($maybe, Thing::class, true) &&
701 240
                                        $val['@context'] === $maybe::SCHEMA_ORG_CONTEXT &&
702 240
                                        $val['@type'] === $maybe::SCHEMA_ORG_TYPE
703
                                    ) {
704 240
                                        return $maybe::DaftObjectFromJsonArray($val);
705
                                    }
706
                                }
707
                            }
708
709 240
                            return $val;
710 240
                        },
711 240
                        $array[$k]
712
                    );
713
                }
714
715 240
                return $array[$k];
716 240
            },
717 240
            $array_keys
718
        ));
719
720
        /**
721
        * @psalm-var Thing
722
        *
723
        * @var Thing
724
        */
725 240
        $out = new $type($data, $writeAll);
726
727 240
        return $out;
728
    }
729
730
    /**
731
    * @param array<int, int|float|string|Thing|DataTypes\DataType> $value
732
    *
733
    * @psalm-param class-string<Thing>|class-string<DataTypes\DataType> ...$implementation
734
    */
735 6
    protected function NudgePropertyWithUniqueNumericsOrThings(
736
        string $property,
737
        string $method,
738
        array $value,
739
        string ...$implementation
0 ignored issues
show
Unused Code introduced by
The parameter $implementation is not used and could be removed. ( Ignorable by Annotation )

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

739
        /** @scrutinizer ignore-unused */ string ...$implementation

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
740
    ) : void {
741 6
        $initialCount = count($value);
742
743
        /**
744
        * @var array<int, string|Thing|DataTypes\DataType>
745
        */
746 6
        $value = array_values(
747 6
            array_map(
748
                /**
749
                * @param string|int|float|Thing|DataTypes\DataType $val
750
                *
751
                * @return int|float|Thing|DataTypes\DataType
752
                */
753
                function ($val) {
754
                    return
755 6
                        is_string($val)
756
                            ? (
757
                                ctype_digit($val)
758
                                    ? (int) $val
759
                                    : (float) $val
760
                            )
761 6
                            : $val;
762 6
                },
763 6
                $value
764
            )
765
        );
766
767 6
        if (count($value) !== $initialCount) {
768
            throw new InvalidArgumentException(
769
                'Arguments passed to ' .
770
                __METHOD__ .
771
                ' must be numerics or instances of ' .
772
                Thing::class
773
            );
774
        }
775
776 6
        $this->NudgePropertyWithUniqueValues($property, $method, $value, SORT_NUMERIC);
777 6
    }
778
779
    /**
780
    * @param array<int, string> $value
781
    */
782 239
    protected function NudgePropertyWithUniqueTrimmedStringsMightNotBeString(
783
        string $property,
784
        string $method,
785
        array $value
786
    ) : void {
787 239
        $initialCount = count($value);
788
789
        $value = array_filter(array_map('trim', $value), function (string $maybe) : bool {
790 238
            return '' !== $maybe;
791 239
        });
792
793 239
        if ($initialCount !== count($value)) {
794
            throw new InvalidArgumentException(
795
                'Arguments passed to ' .
796
                $method .
797
                ' must not have trailing whitespace!'
798
            );
799
        }
800
801 239
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
802 239
    }
803
804
    /**
805
    * @param array<int, int|float|string> $value
806
    */
807 3
    protected function NudgePropertyWithUniqueTrimmedStringsNumericsMightNotBeStringsOrNumerics(
808
        string $property,
809
        string $method,
810
        array $value
811
    ) : void {
812 3
        $initialCount = count($value);
813
814
        /**
815
        * @var array<int, string|float|int>
816
        */
817 3
        $value = array_filter(
818 3
            $value,
819
            /**
820
            * @param mixed $maybe
821
            */
822
            function ($maybe) : bool {
823 3
                return is_string($maybe) || is_numeric($maybe);
824 3
            }
825
        );
826
827 3
        if (count($value) !== $initialCount) {
828
            throw new InvalidArgumentException(
829
                'Argument 1 passed to ' .
830
                $method .
831
                ' must be an array of numerics!'
832
            );
833
        }
834
835
        /**
836
        * @var array<int, string|int|float>
837
        */
838 3
        $value = array_unique(
839 3
            array_map(
840
                /**
841
                * @param string|float|int $val
842
                *
843
                * @return string|float|int
844
                */
845
                function ($val) {
846
                    return
847 3
                        is_string($val)
848
                            ? (
849 3
                                ctype_digit($val)
850
                                    ? (int) $val
851
                                    : (
852 3
                                        is_numeric($val)
853
                                            ? (float) $val
854 3
                                            : trim($val)
855
                                    )
856
                            )
857 3
                            : $val;
858 3
                },
859 3
                $value
860
            ),
861 3
            SORT_NUMERIC
862
        );
863
864 3
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
865 3
    }
866
867
    /**
868
    * @param array<int, int> $value
869
    */
870 73
    protected function NudgePropertyWithUniqueIntegers(
871
        string $property,
872
        string $method,
873
        array $value
874
    ) : void {
875 73
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
876 73
    }
877
878
    /**
879
    * @param array<int, int|float|string> $value
880
    */
881 10
    protected function NudgePropertyWithUniqueIntegersOrFloats(
882
        string $property,
883
        string $method,
884
        array $value
885
    ) : void {
886 10
        $initialCount = count($value);
887
888
        /**
889
        * @var array<int, int|float>
890
        */
891 10
        $value = array_map(
892
            /**
893
            * @param int|float|string $val
894
            *
895
            * @return int|float
896
            */
897
            function ($val) {
898
                return
899 10
                    (is_int($val) || is_float($val))
900 10
                        ? $val
901
                        : (
902
                            ctype_digit($val)
903
                                ? (int) $val
904 10
                                : (float) $val
905
                        );
906 10
            },
907 10
            array_filter($value, 'is_numeric')
908
        );
909
910 10
        if (count($value) !== $initialCount) {
911
            throw new InvalidArgumentException(
912
                'Argument 1 passed to ' .
913
                $method .
914
                ' must be a numeric list!'
915
            );
916
        }
917
918 10
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
919 10
    }
920
921
    /**
922
    * @param array<int, bool> $value
923
    */
924 69
    protected function NudgePropertyWithUniqueBooleans(
925
        string $property,
926
        array $value
927
    ) : void {
928 69
        $replace = [];
929 69
        if (in_array(true, $value, true)) {
930 69
            $replace[] = true;
931
        }
932 69
        if (in_array(false, $value, true)) {
933
            $replace[] = false;
934
        }
935
936 69
        $this->NudgePropertyValue($property, $replace);
937 69
    }
938
939
    /**
940
    * @param array<int, int|string> $value
941
    */
942 2
    protected function NudgePropertWithUniqueIntegersOrTrimmedStrings(
943
        string $property,
944
        string $method,
945
        array $value
946
    ) : void {
947 2
        $initialCount = count($value);
948
949 2
        $value = array_map(
950
            /**
951
            * @param int|string $val
952
            *
953
            * @return int|string
954
            */
955
            function ($val) {
956 2
                return is_string($val) ? trim($val) : $val;
957 2
            },
958 2
            array_filter(
959 2
                $value,
960
                /**
961
                * @param mixed $maybe
962
                */
963
                function ($maybe) : bool {
964 2
                    return is_string($maybe) || is_int($maybe);
965 2
                }
966
            )
967
        );
968
969 2
        if (count($value) !== $initialCount) {
970
            throw new InvalidArgumentException(
971
                'Argument 1 passed to ' .
972
                $method .
973
                ' must be a list of integers & strings!'
974
            );
975
        }
976
977 2
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
978 2
    }
979
980
    /**
981
    * @param array<int, scalar|array|object|null> $value
982
    */
983 239
    protected function NudgePropertyWithUniqueValues(
984
        string $property,
985
        string $method,
986
        array $value,
987
        int $sort_flags = SORT_REGULAR
0 ignored issues
show
Unused Code introduced by
The parameter $sort_flags is not used and could be removed. ( Ignorable by Annotation )

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

987
        /** @scrutinizer ignore-unused */ int $sort_flags = SORT_REGULAR

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
988
    ) : void {
989 239
        $initialCount = count($value);
990
991 239
        $out = [];
992
993 239
        foreach ($value as $val) {
994 239
            if ( ! in_array($val, $out, true)) {
995 239
                $out[] = $val;
996
            } else {
997
                throw new InvalidArgumentException(
998
                    'Arguments passed to ' .
999
                    $method .
1000
                    ' must be unique, ' .
1001
                    var_export($val, true) .
1002 239
                    ' already found!'
1003
                );
1004
            }
1005
        }
1006
1007 239
        $value = $out;
1008
1009 239
        if ($initialCount !== count($value)) {
1010
            throw new InvalidArgumentException(
1011
                'Arguments passed to ' .
1012
                $method .
1013
                ' must be unique!'
1014
            );
1015
        }
1016
1017 239
        $this->NudgePropertyValue($property, $value);
1018 239
    }
1019
1020
    /**
1021
    * @param array<int, Date> $value
1022
    */
1023 4
    protected function NudgePropertyWithUniqueDates(
1024
        string $property,
1025
        string $method,
1026
        array $value
1027
    ) : void {
1028 4
        $this->NudgePropertyWithUniqueValuesOfThings($property, $method, $value, Date::class);
1029 4
    }
1030
1031
    /**
1032
    * @param array<int, DateTime> $value
1033
    */
1034
    protected function NudgePropertyWithUniqueDateTimes(
1035
        string $property,
1036
        string $method,
1037
        array $value
1038
    ) : void {
1039
        $this->NudgePropertyWithUniqueValuesOfThings($property, $method, $value, DateTime::class);
1040
    }
1041
1042
    /**
1043
    * @param array<int, Date|DateTime> $value
1044
    */
1045 3
    protected function NudgePropertyWithUniqueDatesOrDateTimes(
1046
        string $property,
1047
        string $method,
1048
        array $value
1049
    ) : void {
1050 3
        $this->NudgePropertyWithUniqueValuesOfThings(
1051 3
            $property,
1052 3
            $method,
1053 3
            $value,
1054 3
            Date::class,
1055 3
            DateTime::class
1056
        );
1057 3
    }
1058
1059
    /**
1060
    * @param array<int, Person> $value
1061
    */
1062 5
    protected function NudgePropertyWithUniquePersons(
1063
        string $property,
1064
        string $method,
1065
        array $value
1066
    ) : void {
1067 5
        $this->NudgePropertyWithUniqueValuesOfThings(
1068 5
            $property,
1069 5
            $method,
1070 5
            $value,
1071 5
            Person::class
1072
        );
1073 5
    }
1074
1075
    /**
1076
    * @param array<int, Organization|Person> $value
1077
    */
1078 84
    protected function NudgePropertyWithUniqueOrganizationsOrPersons(
1079
        string $property,
1080
        string $method,
1081
        array $value
1082
    ) : void {
1083 84
        $this->NudgePropertyWithUniqueValuesOfThings(
1084 84
            $property,
1085 84
            $method,
1086 84
            $value,
1087 84
            Organization::class,
1088 84
            Person::class
1089
        );
1090 84
    }
1091
1092
    /**
1093
    * @param array<int, Organization> $value
1094
    */
1095 1
    protected function NudgePropertyWithUniqueOrganizations(
1096
        string $property,
1097
        string $method,
1098
        array $value
1099
    ) : void {
1100 1
        $this->NudgePropertyWithUniqueValuesOfThings(
1101 1
            $property,
1102 1
            $method,
1103 1
            $value,
1104 1
            Organization::class
1105
        );
1106 1
    }
1107
1108
    /**
1109
    * @param array<int, Event> $value
1110
    */
1111 4
    protected function NudgePropertyWithUniqueEvents(
1112
        string $property,
1113
        string $method,
1114
        array $value
1115
    ) : void {
1116 4
        $this->NudgePropertyWithUniqueValuesOfThings(
1117 4
            $property,
1118 4
            $method,
1119 4
            $value,
1120 4
            Event::class
1121
        );
1122 4
    }
1123
1124
    /**
1125
    * @param array<int, Place> $value
1126
    */
1127 5
    protected function NudgePropertyValueWithUniquePlaces(
1128
        string $property,
1129
        string $method,
0 ignored issues
show
Unused Code introduced by
The parameter $method is not used and could be removed. ( Ignorable by Annotation )

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

1129
        /** @scrutinizer ignore-unused */ string $method,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1130
        array $value
1131
    ) : void {
1132 5
        $this->NudgePropertyWithUniqueValuesOfThings(
1133 5
            $property,
1134 5
            __METHOD__,
1135 5
            $value,
1136 5
            Place::class
1137
        );
1138 5
    }
1139
1140
    /**
1141
    * @param array<int, ContactPoint|Place> $value
1142
    */
1143 1
    protected function NudgePropertyValueWithUniqueContactPointsOrPlaces(
1144
        string $property,
1145
        string $method,
0 ignored issues
show
Unused Code introduced by
The parameter $method is not used and could be removed. ( Ignorable by Annotation )

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

1145
        /** @scrutinizer ignore-unused */ string $method,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1146
        array $value
1147
    ) : void {
1148 1
        $this->NudgePropertyWithUniqueValuesOfThings(
1149 1
            $property,
1150 1
            __METHOD__,
1151 1
            $value,
1152 1
            ContactPoint::class,
1153 1
            Place::class
1154
        );
1155 1
    }
1156
1157
    /**
1158
    * @param array<int, Thing|DataTypes\DataType> $value
1159
    *
1160
    * @psalm-param class-string<Thing>|class-string<DataTypes\DataType> $validThing
1161
    * @psalm-param class-string<Thing>|class-string<DataTypes\DataType> ...$validThings
1162
    */
1163 239
    protected function NudgePropertyWithUniqueValuesOfThings(
1164
        string $property,
1165
        string $method,
1166
        array $value,
1167
        string $validThing,
1168
        string ...$validThings
1169
    ) : void {
1170 239
        array_unshift($validThings, $validThing);
1171
1172 239
        $initialCount = count($validThings);
1173
1174 239
        if (count($validThings) !== $initialCount) {
1175
            throw new InvalidArgumentException(
1176
                'Arguments 4+ passed to ' .
1177
                __METHOD__ .
1178
                ' must be implementations of ' .
1179
                Thing::class .
1180
                '!'
1181
            );
1182
        }
1183
1184 239
        $initialCount = count($value);
1185
1186
        /**
1187
        * @var array<int, Thing|DataTypes\DataType>
1188
        */
1189 239
        $value = array_filter(
1190 239
            $value,
1191
            /**
1192
            * @param Thing|DataTypes\DataType $maybe
1193
            */
1194
            function ($maybe) use ($validThings) : bool {
1195 238
                foreach ($validThings as $thing) {
1196 238
                    if (is_a($maybe, $thing, true)) {
1197 238
                        return true;
1198
                    }
1199
                }
1200
1201
                return false;
1202 239
            }
1203
        );
1204
1205 239
        if (count($value) !== $initialCount) {
1206
            throw new InvalidArgumentException(
1207
                'Argument 1 passed to ' .
1208
                $method .
1209
                ' must be an array containing any combination of ' .
1210
                implode(', ', $validThings)
1211
            );
1212
        }
1213
1214 239
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
1215 239
    }
1216
}
1217