Passed
Push — master ( 79069a...205e6e )
by SignpostMarv
11:50
created

Thing   F

Complexity

Total Complexity 98

Size/Duplication

Total Lines 1242
Duplicated Lines 0 %

Test Coverage

Coverage 77.78%

Importance

Changes 0
Metric Value
eloc 425
dl 0
loc 1242
ccs 343
cts 441
cp 0.7778
rs 2
c 0
b 0
f 0
wmc 98

58 Methods

Rating   Name   Duplication   Size   Complexity  
A GetUrl() 0 12 1
A SetImage() 0 7 1
A GetName() 0 12 1
A GetSubjectOf() 0 12 1
A NudgePropertyWithUniqueIntegersOrFloats() 0 38 5
A __construct() 0 23 1
A DaftObjectExportableProperties() 0 3 1
A ObtainType() 0 3 1
A SetIdentifier() 0 6 1
A GetImage() 0 12 1
A DaftObjectNullableProperties() 0 19 1
A SetDisambiguatingDescription() 0 6 1
A SetSameAs() 0 3 1
A jsonSerialize() 0 9 2
A SetUrl() 0 3 1
A GetPotentialAction() 0 12 1
A GetAdditionalType() 0 12 1
A ObtainContext() 0 3 1
A GetDisambiguatingDescription() 0 12 1
A DaftObjectPublicGetters() 0 18 2
A SetPotentialAction() 0 7 1
A DaftObjectPublicOrProtectedGetters() 0 18 2
B DaftObjectFromJsonArray() 0 69 9
A GetIdentifier() 0 12 1
A NudgePropertyWithUniqueBooleansOrThings() 0 7 1
A SetAlternateName() 0 6 1
A DaftObjectPropertiesWithMultiTypedArraysOfUniqueValues() 0 23 2
A DaftObjectJsonProperties() 0 3 1
A SetMainEntityOfPage() 0 7 1
A SetAdditionalType() 0 6 1
A SetDescription() 0 6 1
A DaftObjectFromJsonString() 0 15 1
A DaftObjectPublicSetters() 0 18 2
A SetName() 0 3 1
A GetAlternateName() 0 12 1
A GetDescription() 0 12 1
A SetSubjectOf() 0 3 1
A DaftObjectProperties() 0 19 1
A GetMainEntityOfPage() 0 12 1
A GetSameAs() 0 12 1
A NudgePropertyWithUniqueNumericsOrThings() 0 42 4
A NudgePropertyWithUniqueTrimmedStringsMightNotBeString() 0 20 2
A NudgePropertyWithUniqueIntegers() 0 6 1
A NudgePropertyWithUniqueBooleans() 0 13 3
A NudgePropertyWithUniqueTrimmedStringsOrThings() 0 41 3
A NudgePropertyWithUniqueValuesOfThings() 0 52 5
A NudgePropertyWithUniqueOrganizationsOrPersons() 0 11 1
A NudgePropertyWithUniqueDates() 0 6 1
A NudgePropertyWithUniqueOrganizations() 0 10 1
A NudgePropertyValueWithUniqueContactPointsOrPlaces() 0 11 1
A NudgePropertyWithUniqueDateTimes() 0 6 1
A NudgePropertyWithUniqueValues() 0 35 4
A NudgePropertyWithUniqueEvents() 0 10 1
A NudgePropertyWithUniquePersons() 0 10 1
A NudgePropertyWithUniqueDatesOrDateTimes() 0 11 1
A NudgePropertWithUniqueIntegersOrTrimmedStrings() 0 36 4
A NudgePropertyValueWithUniquePlaces() 0 10 1
B NudgePropertyWithUniqueTrimmedStringsNumericsMightNotBeStringsOrNumerics() 0 58 6

How to fix   Complexity   

Complex Class

Complex classes like Thing often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Thing, and based on these observations, apply Extract Interface, too.

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

741
        /** @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...
742
    ) : void {
743
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
744
    }
745
746
    /**
747
    * @param array<int, string|Thing|DataTypes\DataType> $value
748
    *
749
    * @psalm-param class-string<Thing>|class-string<DataTypes\DataType> ...$implementation
750
    */
751 198
    protected function NudgePropertyWithUniqueTrimmedStringsOrThings(
752
        string $property,
753
        string $method,
754
        array $value,
755
        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

755
        /** @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...
756
    ) : void {
757 198
        $initialCount = count($value);
758
759
        /**
760
        * @var array<int, string|Thing|DataTypes\DataType>
761
        */
762 198
        $value = array_values(array_filter(
763 198
            array_map(
764
                /**
765
                * @param string|Thing|DataTypes\DataType $val
766
                *
767
                * @return string|Thing|DataTypes\DataType
768
                */
769
                function ($val) {
770 197
                    return is_string($val) ? trim($val) : $val;
771 198
                },
772 198
                $value
773
            ),
774
            /**
775
            * @param string|Thing|DataTypes\DataType $maybe
776
            */
777
            function ($maybe) : bool {
778 197
                return '' !== $maybe;
779 198
            }
780
        ));
781
782 198
        if (count($value) !== $initialCount) {
783
            throw new InvalidArgumentException(
784
                'Arguments passed to ' .
785
                __METHOD__ .
786
                ' must be strings with no trailing whitespace or instances of ' .
787
                Thing::class
788
            );
789
        }
790
791 198
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
792 198
    }
793
794
    /**
795
    * @param array<int, int|float|string|Thing|DataTypes\DataType> $value
796
    *
797
    * @psalm-param class-string<Thing>|class-string<DataTypes\DataType> ...$implementation
798
    */
799
    protected function NudgePropertyWithUniqueNumericsOrThings(
800
        string $property,
801
        string $method,
802
        array $value,
803
        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

803
        /** @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...
804
    ) : void {
805
        $initialCount = count($value);
806
807
        /**
808
        * @var array<int, string|Thing|DataTypes\DataType>
809
        */
810
        $value = array_values(
811
            array_map(
812
                /**
813
                * @param string|int|float|Thing|DataTypes\DataType $val
814
                *
815
                * @return int|float|Thing|DataTypes\DataType
816
                */
817
                function ($val) {
818
                    return
819
                        is_string($val)
820
                            ? (
821
                                ctype_digit($val)
822
                                    ? (int) $val
823
                                    : (float) $val
824
                            )
825
                            : $val;
826
                },
827
                $value
828
            )
829
        );
830
831
        if (count($value) !== $initialCount) {
832
            throw new InvalidArgumentException(
833
                'Arguments passed to ' .
834
                __METHOD__ .
835
                ' must be numerics or instances of ' .
836
                Thing::class
837
            );
838
        }
839
840
        $this->NudgePropertyWithUniqueValues($property, $method, $value, SORT_NUMERIC);
841
    }
842
843
    /**
844
    * @param array<int, string> $value
845
    */
846 198
    protected function NudgePropertyWithUniqueTrimmedStringsMightNotBeString(
847
        string $property,
848
        string $method,
849
        array $value
850
    ) : void {
851 198
        $initialCount = count($value);
852
853
        $value = array_filter(array_map('trim', $value), function (string $maybe) : bool {
854 197
            return '' !== $maybe;
855 198
        });
856
857 198
        if ($initialCount !== count($value)) {
858
            throw new InvalidArgumentException(
859
                'Arguments passed to ' .
860
                $method .
861
                ' must not have trailing whitespace!'
862
            );
863
        }
864
865 198
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
866 198
    }
867
868
    /**
869
    * @param array<int, int|float|string> $value
870
    */
871 2
    protected function NudgePropertyWithUniqueTrimmedStringsNumericsMightNotBeStringsOrNumerics(
872
        string $property,
873
        string $method,
874
        array $value
875
    ) : void {
876 2
        $initialCount = count($value);
877
878
        /**
879
        * @var array<int, string|float|int>
880
        */
881 2
        $value = array_filter(
882 2
            $value,
883
            /**
884
            * @param mixed $maybe
885
            */
886
            function ($maybe) : bool {
887 2
                return is_string($maybe) || is_numeric($maybe);
888 2
            }
889
        );
890
891 2
        if (count($value) !== $initialCount) {
892
            throw new InvalidArgumentException(
893
                'Argument 1 passed to ' .
894
                $method .
895
                ' must be an array of numerics!'
896
            );
897
        }
898
899
        /**
900
        * @var array<int, string|int|float>
901
        */
902 2
        $value = array_unique(
903 2
            array_map(
904
                /**
905
                * @param string|float|int $val
906
                *
907
                * @return string|float|int
908
                */
909
                function ($val) {
910
                    return
911 2
                        is_string($val)
912
                            ? (
913 2
                                ctype_digit($val)
914
                                    ? (int) $val
915
                                    : (
916 2
                                        is_numeric($val)
917
                                            ? (float) $val
918 2
                                            : trim($val)
919
                                    )
920
                            )
921 2
                            : $val;
922 2
                },
923 2
                $value
924
            ),
925 2
            SORT_NUMERIC
926
        );
927
928 2
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
929 2
    }
930
931
    /**
932
    * @param array<int, int> $value
933
    */
934 67
    protected function NudgePropertyWithUniqueIntegers(
935
        string $property,
936
        string $method,
937
        array $value
938
    ) : void {
939 67
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
940 67
    }
941
942
    /**
943
    * @param array<int, int|float|string> $value
944
    */
945 5
    protected function NudgePropertyWithUniqueIntegersOrFloats(
946
        string $property,
947
        string $method,
948
        array $value
949
    ) : void {
950 5
        $initialCount = count($value);
951
952
        /**
953
        * @var array<int, int|float>
954
        */
955 5
        $value = array_map(
956
            /**
957
            * @param int|float|string $val
958
            *
959
            * @return int|float
960
            */
961
            function ($val) {
962
                return
963 5
                    (is_int($val) || is_float($val))
964 5
                        ? $val
965
                        : (
966
                            ctype_digit($val)
967
                                ? (int) $val
968 5
                                : (float) $val
969
                        );
970 5
            },
971 5
            array_filter($value, 'is_numeric')
972
        );
973
974 5
        if (count($value) !== $initialCount) {
975
            throw new InvalidArgumentException(
976
                'Argument 1 passed to ' .
977
                $method .
978
                ' must be a numeric list!'
979
            );
980
        }
981
982 5
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
983 5
    }
984
985
    /**
986
    * @param array<int, bool> $value
987
    */
988 65
    protected function NudgePropertyWithUniqueBooleans(
989
        string $property,
990
        array $value
991
    ) : void {
992 65
        $replace = [];
993 65
        if (in_array(true, $value, true)) {
994 65
            $replace[] = true;
995
        }
996 65
        if (in_array(false, $value, true)) {
997
            $replace[] = false;
998
        }
999
1000 65
        $this->NudgePropertyValue($property, $replace);
1001 65
    }
1002
1003
    /**
1004
    * @param array<int, int|string> $value
1005
    */
1006 1
    protected function NudgePropertWithUniqueIntegersOrTrimmedStrings(
1007
        string $property,
1008
        string $method,
1009
        array $value
1010
    ) : void {
1011 1
        $initialCount = count($value);
1012
1013 1
        $value = array_map(
1014
            /**
1015
            * @param int|string $val
1016
            *
1017
            * @return int|string
1018
            */
1019
            function ($val) {
1020 1
                return is_string($val) ? trim($val) : $val;
1021 1
            },
1022 1
            array_filter(
1023 1
                $value,
1024
                /**
1025
                * @param mixed $maybe
1026
                */
1027
                function ($maybe) : bool {
1028 1
                    return is_string($maybe) || is_int($maybe);
1029 1
                }
1030
            )
1031
        );
1032
1033 1
        if (count($value) !== $initialCount) {
1034
            throw new InvalidArgumentException(
1035
                'Argument 1 passed to ' .
1036
                $method .
1037
                ' must be a list of integers & strings!'
1038
            );
1039
        }
1040
1041 1
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
1042 1
    }
1043
1044
    /**
1045
    * @param array<int, scalar|array|object|null> $value
1046
    */
1047 198
    protected function NudgePropertyWithUniqueValues(
1048
        string $property,
1049
        string $method,
1050
        array $value,
1051
        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

1051
        /** @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...
1052
    ) : void {
1053 198
        $initialCount = count($value);
1054
1055 198
        $out = [];
1056
1057 198
        foreach ($value as $val) {
1058 198
            if ( ! in_array($val, $out, true)) {
1059 198
                $out[] = $val;
1060
            } else {
1061
                throw new InvalidArgumentException(
1062
                    'Arguments passed to ' .
1063
                    $method .
1064
                    ' must be unique, ' .
1065
                    var_export($val, true) .
1066 198
                    ' already found!'
1067
                );
1068
            }
1069
        }
1070
1071 198
        $value = $out;
1072
1073 198
        if ($initialCount !== count($value)) {
1074
            throw new InvalidArgumentException(
1075
                'Arguments passed to ' .
1076
                $method .
1077
                ' must be unique!'
1078
            );
1079
        }
1080
1081 198
        $this->NudgePropertyValue($property, $value);
1082 198
    }
1083
1084
    /**
1085
    * @param array<int, Date> $value
1086
    */
1087
    protected function NudgePropertyWithUniqueDates(
1088
        string $property,
1089
        string $method,
1090
        array $value
1091
    ) : void {
1092
        $this->NudgePropertyWithUniqueValuesOfThings($property, $method, $value, Date::class);
1093
    }
1094
1095
    /**
1096
    * @param array<int, DateTime> $value
1097
    */
1098
    protected function NudgePropertyWithUniqueDateTimes(
1099
        string $property,
1100
        string $method,
1101
        array $value
1102
    ) : void {
1103
        $this->NudgePropertyWithUniqueValuesOfThings($property, $method, $value, DateTime::class);
1104
    }
1105
1106
    /**
1107
    * @param array<int, Date|DateTime> $value
1108
    */
1109
    protected function NudgePropertyWithUniqueDatesOrDateTimes(
1110
        string $property,
1111
        string $method,
1112
        array $value
1113
    ) : void {
1114
        $this->NudgePropertyWithUniqueValuesOfThings(
1115
            $property,
1116
            $method,
1117
            $value,
1118
            Date::class,
1119
            DateTime::class
1120
        );
1121
    }
1122
1123
    /**
1124
    * @param array<int, Person> $value
1125
    */
1126 1
    protected function NudgePropertyWithUniquePersons(
1127
        string $property,
1128
        string $method,
1129
        array $value
1130
    ) : void {
1131 1
        $this->NudgePropertyWithUniqueValuesOfThings(
1132 1
            $property,
1133 1
            $method,
1134 1
            $value,
1135 1
            Person::class
1136
        );
1137 1
    }
1138
1139
    /**
1140
    * @param array<int, Organization|Person> $value
1141
    */
1142 70
    protected function NudgePropertyWithUniqueOrganizationsOrPersons(
1143
        string $property,
1144
        string $method,
1145
        array $value
1146
    ) : void {
1147 70
        $this->NudgePropertyWithUniqueValuesOfThings(
1148 70
            $property,
1149 70
            $method,
1150 70
            $value,
1151 70
            Organization::class,
1152 70
            Person::class
1153
        );
1154 70
    }
1155
1156
    /**
1157
    * @param array<int, Organization> $value
1158
    */
1159
    protected function NudgePropertyWithUniqueOrganizations(
1160
        string $property,
1161
        string $method,
1162
        array $value
1163
    ) : void {
1164
        $this->NudgePropertyWithUniqueValuesOfThings(
1165
            $property,
1166
            $method,
1167
            $value,
1168
            Organization::class
1169
        );
1170
    }
1171
1172
    /**
1173
    * @param array<int, Event> $value
1174
    */
1175
    protected function NudgePropertyWithUniqueEvents(
1176
        string $property,
1177
        string $method,
1178
        array $value
1179
    ) : void {
1180
        $this->NudgePropertyWithUniqueValuesOfThings(
1181
            $property,
1182
            $method,
1183
            $value,
1184
            Event::class
1185
        );
1186
    }
1187
1188
    /**
1189
    * @param array<int, Place> $value
1190
    */
1191 5
    protected function NudgePropertyValueWithUniquePlaces(
1192
        string $property,
1193
        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

1193
        /** @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...
1194
        array $value
1195
    ) : void {
1196 5
        $this->NudgePropertyWithUniqueValuesOfThings(
1197 5
            $property,
1198 5
            __METHOD__,
1199 5
            $value,
1200 5
            Place::class
1201
        );
1202 5
    }
1203
1204
    /**
1205
    * @param array<int, ContactPoint|Place> $value
1206
    */
1207
    protected function NudgePropertyValueWithUniqueContactPointsOrPlaces(
1208
        string $property,
1209
        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

1209
        /** @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...
1210
        array $value
1211
    ) : void {
1212
        $this->NudgePropertyWithUniqueValuesOfThings(
1213
            $property,
1214
            __METHOD__,
1215
            $value,
1216
            ContactPoint::class,
1217
            Place::class
1218
        );
1219
    }
1220
1221
    /**
1222
    * @param array<int, Thing|DataTypes\DataType> $value
1223
    *
1224
    * @psalm-param class-string<Thing>|class-string<DataTypes\DataType> $validThing
1225
    * @psalm-param class-string<Thing>|class-string<DataTypes\DataType> ...$validThings
1226
    */
1227 198
    protected function NudgePropertyWithUniqueValuesOfThings(
1228
        string $property,
1229
        string $method,
1230
        array $value,
1231
        string $validThing,
1232
        string ...$validThings
1233
    ) : void {
1234 198
        array_unshift($validThings, $validThing);
1235
1236 198
        $initialCount = count($validThings);
1237
1238 198
        if (count($validThings) !== $initialCount) {
1239
            throw new InvalidArgumentException(
1240
                'Arguments 4+ passed to ' .
1241
                __METHOD__ .
1242
                ' must be implementations of ' .
1243
                Thing::class .
1244
                '!'
1245
            );
1246
        }
1247
1248 198
        $initialCount = count($value);
1249
1250
        /**
1251
        * @var array<int, Thing|DataTypes\DataType>
1252
        */
1253 198
        $value = array_filter(
1254 198
            $value,
1255
            /**
1256
            * @param Thing|DataTypes\DataType $maybe
1257
            */
1258
            function ($maybe) use ($validThings) : bool {
1259 197
                foreach ($validThings as $thing) {
1260 197
                    if (is_a($maybe, $thing, true)) {
1261 197
                        return true;
1262
                    }
1263
                }
1264
1265
                return false;
1266 198
            }
1267
        );
1268
1269 198
        if (count($value) !== $initialCount) {
1270
            throw new InvalidArgumentException(
1271
                'Argument 1 passed to ' .
1272
                $method .
1273
                ' must be an array containing any combination of ' .
1274
                implode(', ', $validThings)
1275
            );
1276
        }
1277
1278 198
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
1279 198
    }
1280
}
1281