Passed
Push — master ( 669ccb...85dfa5 )
by SignpostMarv
04:19
created

Thing   F

Complexity

Total Complexity 96

Size/Duplication

Total Lines 1226
Duplicated Lines 0 %

Test Coverage

Coverage 67.82%

Importance

Changes 0
Metric Value
eloc 414
dl 0
loc 1226
ccs 293
cts 432
cp 0.6782
rs 2
c 0
b 0
f 0
wmc 96

58 Methods

Rating   Name   Duplication   Size   Complexity  
A GetUrl() 0 12 1
A NudgePropertyWithUniqueValuesOfThings() 0 52 5
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 NudgePropertWithUniqueIntegersOrTrimmedStrings() 0 36 4
A jsonSerialize() 0 9 2
A SetUrl() 0 3 1
A GetPotentialAction() 0 12 1
A NudgePropertyWithUniqueDates() 0 6 1
A GetAdditionalType() 0 12 1
A NudgePropertyWithUniqueOrganizations() 0 10 1
A ObtainContext() 0 3 1
B NudgePropertyWithUniqueTrimmedStringsNumericsMightNotBeStringsOrNumerics() 0 58 6
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 NudgePropertyValueWithUniqueContactPointsOrPlaces() 0 11 1
A GetIdentifier() 0 12 1
A NudgePropertyWithUniqueDateTimes() 0 6 1
A NudgePropertyWithUniqueBooleansOrThings() 0 7 1
A SetAlternateName() 0 6 1
A DaftObjectPropertiesWithMultiTypedArraysOfUniqueValues() 0 23 2
A NudgePropertyWithUniqueValues() 0 19 2
A NudgePropertyValueWithUniquePlaces() 0 10 1
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 NudgePropertyWithUniqueEvents() 0 10 1
A GetAlternateName() 0 12 1
A NudgePropertyWithUniquePersons() 0 10 1
A GetDescription() 0 12 1
A NudgePropertyWithUniqueDatesOrDateTimes() 0 11 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 NudgePropertyWithUniqueOrganizationsOrPersons() 0 11 1
A NudgePropertyWithUniqueIntegers() 0 6 1
A NudgePropertyWithUniqueBooleans() 0 13 3
A NudgePropertyWithUniqueTrimmedStringsOrThings() 0 41 3

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 350
    public function __construct(array $data = [], bool $writeAll = false)
106
    {
107 350
        $missing = array_diff(static::DaftObjectProperties(), array_keys($data));
108
109
        /**
110
        * @var array<int, string>
111
        */
112 350
        $missing = array_combine(
113 350
            $missing,
114 350
            array_fill(0, count($missing), [])
115
        );
116
117
        /**
118
        * @var array<string, scalar|array|object|null>
119
        */
120 350
        $data = array_merge(
121 350
            $data,
122 350
            $missing
123
        );
124
125 350
        unset($data['@context'], $data['@type']);
126
127 350
        parent::__construct($data, $writeAll);
128 350
    }
129
130 37
    public function ObtainContext() : string
131
    {
132 37
        return (string) static::SCHEMA_ORG_CONTEXT;
133
    }
134
135 37
    public function ObtainType() : string
136
    {
137 37
        return (string) static::SCHEMA_ORG_TYPE;
138
    }
139
140
    /**
141
    * @return array<int, string>
142
    */
143 37
    public function GetAdditionalType() : array
144
    {
145
        /**
146
        * @var array<int, string>
147
        */
148 37
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
149 37
            'additionalType',
150 37
            $this->RetrievePropertyValueFromData('additionalType'),
151 37
            static::class
152
        );
153
154 37
        return $out;
155
    }
156
157
    /**
158
    * @param array<int, string> $value
159
    */
160 1
    public function SetAdditionalType(array $value) : void
161
    {
162 1
        $this->NudgePropertyValue(
163 1
            'additionalType',
164 1
            $value,
165 1
            true
166
        );
167 1
    }
168
169
    /**
170
    * @return array<int, string>
171
    */
172 37
    public function GetAlternateName() : array
173
    {
174
        /**
175
        * @var array<int, string>
176
        */
177 37
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
178 37
            'alternateName',
179 37
            $this->RetrievePropertyValueFromData('alternateName'),
180 37
            static::class
181
        );
182
183 37
        return $out;
184
    }
185
186
    /**
187
    * @param array<int, string> $value
188
    */
189 1
    public function SetAlternateName(array $value) : void
190
    {
191 1
        $this->NudgePropertyValue(
192 1
            'alternateName',
193 1
            $value,
194 1
            true
195
        );
196 1
    }
197
198
    /**
199
    * @return array<int, string>
200
    */
201 37
    public function GetDescription() : array
202
    {
203
        /**
204
        * @var array<int, string>
205
        */
206 37
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
207 37
            'description',
208 37
            $this->RetrievePropertyValueFromData('description'),
209 37
            static::class
210
        );
211
212 37
        return $out;
213
    }
214
215
    /**
216
    * @param array<int, string> $value
217
    */
218 1
    public function SetDescription(array $value) : void
219
    {
220 1
        $this->NudgePropertyValue(
221 1
            'description',
222 1
            $value,
223 1
            true
224
        );
225 1
    }
226
227
    /**
228
    * @return array<int, string>
229
    */
230 37
    public function GetDisambiguatingDescription() : array
231
    {
232
        /**
233
        * @var array<int, string>
234
        */
235 37
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
236 37
            'disambiguatingDescription',
237 37
            $this->RetrievePropertyValueFromData('disambiguatingDescription'),
238 37
            static::class
239
        );
240
241 37
        return $out;
242
    }
243
244
    /**
245
    * @param array<int, string> $value
246
    */
247 1
    public function SetDisambiguatingDescription(array $value) : void
248
    {
249 1
        $this->NudgePropertyValue(
250 1
            'disambiguatingDescription',
251 1
            $value,
252 1
            true
253
        );
254 1
    }
255
256
    /**
257
    * @return array<int, string|PropertyValue>
258
    */
259 40
    public function GetIdentifier() : array
260
    {
261
        /**
262
        * @var array<int, string|PropertyValue>
263
        */
264 40
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
265 40
            'identifier',
266 40
            $this->RetrievePropertyValueFromData('identifier'),
267 40
            static::class
268
        );
269
270 40
        return $out;
271
    }
272
273
    /**
274
    * @param array<int, string|PropertyValue> $value
275
    */
276 4
    public function SetIdentifier(array $value) : void
277
    {
278 4
        $this->NudgePropertyValue(
279 4
            'identifier',
280 4
            $value,
281 4
            true
282
        );
283 4
    }
284
285
    /**
286
    * @return array<int, string|ImageObject>
287
    */
288 37
    public function GetImage() : array
289
    {
290
        /**
291
        * @var array<int, string|ImageObject>
292
        */
293 37
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
294 37
            'image',
295 37
            $this->RetrievePropertyValueFromData('image'),
296 37
            static::class
297
        );
298
299 37
        return $out;
300
    }
301
302
    /**
303
    * @param array<int, string|ImageObject> $value
304
    */
305 1
    public function SetImage(array $value) : void
306
    {
307 1
        $this->NudgePropertyWithUniqueTrimmedStringsOrThings(
308 1
            'image',
309 1
            __METHOD__,
310 1
            $value,
311 1
            ImageObject::class
312
        );
313 1
    }
314
315
    /**
316
    * @return array<int, string|CreativeWork>
317
    */
318 37
    public function GetMainEntityOfPage() : array
319
    {
320
        /**
321
        * @var array<int, string|CreativeWork>
322
        */
323 37
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
324 37
            'mainEntityOfPage',
325 37
            $this->RetrievePropertyValueFromData('mainEntityOfPage'),
326 37
            static::class
327
        );
328
329 37
        return $out;
330
    }
331
332
    /**
333
    * @param array<int, string|CreativeWork> $value
334
    */
335 1
    public function SetMainEntityOfPage(array $value) : void
336
    {
337 1
        $this->NudgePropertyWithUniqueTrimmedStringsOrThings(
338 1
            'mainEntityOfPage',
339 1
            __METHOD__,
340 1
            $value,
341 1
            CreativeWork::class
342
        );
343 1
    }
344
345
    /**
346
    * @return array<int, string>
347
    */
348 53
    public function GetName() : array
349
    {
350
        /**
351
        * @var array<int, string>
352
        */
353 53
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
354 53
            'name',
355 53
            $this->RetrievePropertyValueFromData('name'),
356 53
            static::class
357
        );
358
359 53
        return $out;
360
    }
361
362
    /**
363
    * @param array<int, string> $value
364
    */
365 17
    public function SetName(array $value) : void
366
    {
367 17
        $this->NudgePropertyWithUniqueTrimmedStringsMightNotBeString('name', __METHOD__, $value);
368 17
    }
369
370
    /**
371
    * @return array<int, Action>
372
    */
373 37
    public function GetPotentialAction() : array
374
    {
375
        /**
376
        * @var array<int, Action>
377
        */
378 37
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
379 37
            'potentialAction',
380 37
            $this->RetrievePropertyValueFromData('potentialAction'),
381 37
            static::class
382
        );
383
384 37
        return $out;
385
    }
386
387
    /**
388
    * @param array<int, Action> $value
389
    */
390 1
    public function SetPotentialAction(array $value) : void
391
    {
392 1
        $this->NudgePropertyWithUniqueValuesOfThings(
393 1
            'potentialAction',
394 1
            __METHOD__,
395 1
            $value,
396 1
            Action::class
397
        );
398 1
    }
399
400
    /**
401
    * @return array<int, string>
402
    */
403 37
    public function GetSameAs() : array
404
    {
405
        /**
406
        * @var array<int, string>
407
        */
408 37
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
409 37
            'sameAs',
410 37
            $this->RetrievePropertyValueFromData('sameAs'),
411 37
            static::class
412
        );
413
414 37
        return $out;
415
    }
416
417
    /**
418
    * @param array<int, string> $value
419
    */
420 1
    public function SetSameAs(array $value) : void
421
    {
422 1
        $this->NudgePropertyWithUniqueTrimmedStringsMightNotBeString('sameAs', __METHOD__, $value);
423 1
    }
424
425
    /**
426
    * @return array<int, CreativeWork|Event>
427
    */
428 37
    public function GetSubjectOf() : array
429
    {
430
        /**
431
        * @var array<int, CreativeWork|Event>
432
        */
433 37
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
434 37
            'subjectOf',
435 37
            $this->RetrievePropertyValueFromData('subjectOf'),
436 37
            static::class
437
        );
438
439 37
        return $out;
440
    }
441
442
    /**
443
    * @param array<int, CreativeWork|Event> $value
444
    */
445 1
    public function SetSubjectOf(array $value) : void
446
    {
447 1
        $this->NudgePropertyValue('subjectOf', $value, true);
448 1
    }
449
450
    /**
451
    * @return array<int, string>
452
    */
453 37
    public function GetUrl() : array
454
    {
455
        /**
456
        * @var array<int, string>
457
        */
458 37
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
459 37
            'url',
460 37
            $this->RetrievePropertyValueFromData('url'),
461 37
            static::class
462
        );
463
464 37
        return $out;
465
    }
466
467
    /**
468
    * @param array<int, string> $value
469
    */
470 1
    public function SetUrl(array $value) : void
471
    {
472 1
        $this->NudgePropertyWithUniqueTrimmedStringsMightNotBeString('url', __METHOD__, $value);
473 1
    }
474
475 37
    public function jsonSerialize() : array
476
    {
477 37
        return array_filter(
478 37
            parent::jsonSerialize(),
479
            /**
480
            * @param scalar|array|object|null $val
481
            */
482
            function ($val) : bool {
483 37
                return ! is_array($val) || count($val) > 0;
484 37
            }
485
        );
486
    }
487
488 10756
    public static function DaftObjectProperties() : array
489
    {
490
        /**
491
        * @var array<int, string>
492
        */
493 10756
        $static = static::PROPERTIES;
494
495
        /**
496
        * @var string
497
        *
498
        * @psalm-var class-string<Thing>
499
        */
500 10756
        $static_parent = get_parent_class(static::class);
501
502 10756
        $parent = $static_parent::DaftObjectProperties();
503
504 10756
        return array_unique(array_merge(
505 10756
            $parent,
506 10756
            $static
507
        ));
508
    }
509
510
    /**
511
    * @return array<string, array<int, string>>
512
    */
513 73
    public static function DaftObjectPropertiesWithMultiTypedArraysOfUniqueValues() : array
514
    {
515
        /**
516
        * @var array<string, array<int, string>>
517
        */
518 73
        $static = static::PROPERTIES_WITH_MULTI_TYPED_ARRAYS;
519
520
        /**
521
        * @var string
522
        *
523
        * @psalm-var class-string<Thing>
524
        */
525 73
        $static_parent = get_parent_class(static::class);
526
527 73
        if ($static_parent === get_parent_class(self::class)) {
528 73
            return $static;
529
        }
530
531 73
        $parent = $static_parent::DaftObjectPropertiesWithMultiTypedArraysOfUniqueValues();
532
533 73
        return array_merge(
534 73
            $parent,
535 73
            $static
536
        );
537
    }
538
539 462
    public static function DaftObjectNullableProperties() : array
540
    {
541
        /**
542
        * @var array<int, string>
543
        */
544 462
        $static = static::NULLABLE_PROPERTIES;
545
546
        /**
547
        * @var string
548
        *
549
        * @psalm-var class-string<Thing>
550
        */
551 462
        $static_parent = get_parent_class(static::class);
552
553 462
        $parent = $static_parent::DaftObjectNullableProperties();
554
555 462
        return array_unique(array_merge(
556 462
            $parent,
557 462
            $static
558
        ));
559
    }
560
561 260
    public static function DaftObjectExportableProperties() : array
562
    {
563 260
        return static::DaftObjectProperties();
564
    }
565
566 73
    public static function DaftObjectJsonProperties() : array
567
    {
568 73
        return static::DaftObjectProperties();
569
    }
570
571 73
    public static function DaftObjectPublicGetters() : array
572
    {
573
        /**
574
        * @var string
575
        *
576
        * @psalm-var class-string<Thing>
577
        */
578 73
        $static_parent = get_parent_class(static::class);
579
580 73
        $static = TypeUtilities::DaftObjectPublicGetters(static::class);
581
582 73
        if ($static_parent === get_parent_class(self::class)) {
583 1
            return $static;
584
        }
585
586 73
        return array_unique(array_merge(
587 73
            TypeUtilities::DaftObjectPublicGetters($static_parent),
588 73
            $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 37
    public static function DaftObjectPublicSetters() : array
614
    {
615
        /**
616
        * @var string
617
        *
618
        * @psalm-var class-string<Thing>
619
        */
620 37
        $static_parent = get_parent_class(static::class);
621
622 37
        $static = TypeUtilities::DaftObjectPublicSetters(static::class);
623
624 37
        if ($static_parent === get_parent_class(self::class)) {
625 1
            return $static;
626
        }
627
628 37
        return array_unique(array_merge(
629 37
            TypeUtilities::DaftObjectPublicSetters($static_parent),
630 37
            $static
631
        ));
632
    }
633
634
    /**
635
    * @return static
636
    *
637
    * @psalm-return Thing
638
    */
639 37
    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 37
        $decoded = json_decode($string, true);
647
648
        /**
649
        * @var static
650
        */
651 37
        $out = static::DaftObjectFromJsonArray($decoded, $writeAll);
652
653 37
        return $out;
654
    }
655
656
    /**
657
    * @return static
658
    *
659
    * @psalm-return Thing
660
    */
661 37
    public static function DaftObjectFromJsonArray(
662
        array $array,
663
        bool $writeAll = self::BOOL_DEFAULT_WRITEALL
664
    ) : DaftJson {
665 37
        $type = JsonTypeUtilities::ThrowIfNotDaftJson(static::class);
666
667 37
        $multi_type = static::DaftObjectPropertiesWithMultiTypedArraysOfUniqueValues();
668
669 37
        $data = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
670
671 37
        $array_keys = array_keys($array);
672
673 37
        $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 37
                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 37
                if (is_array($array[$k])) {
689 37
                    return array_map(
690
                        /**
691
                        * @param mixed $val
692
                        *
693
                        * @return mixed
694
                        */
695
                        function ($val) use ($k, $multi_type) {
696
                            if (
697 37
                                is_array($val) &&
698 37
                                isset($val['@context'], $val['@type'], $multi_type[$k])
699
                            ) {
700 17
                                foreach ($multi_type[$k] as $maybe) {
701
                                    if (
702 17
                                        is_a($maybe, Thing::class, true) &&
703 17
                                        $val['@context'] === $maybe::SCHEMA_ORG_CONTEXT &&
704 17
                                        $val['@type'] === $maybe::SCHEMA_ORG_TYPE
705
                                    ) {
706 17
                                        return $maybe::DaftObjectFromJsonArray($val);
707
                                    }
708
                                }
709
                            }
710
711 37
                            return $val;
712 37
                        },
713 37
                        $array[$k]
714
                    );
715
                }
716
717 37
                return $array[$k];
718 37
            },
719 37
            $array_keys
720
        ));
721
722
        /**
723
        * @psalm-var Thing
724
        *
725
        * @var Thing
726
        */
727 37
        $out = new $type($data, $writeAll);
728
729 37
        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 2
    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 2
        $initialCount = count($value);
758
759
        /**
760
        * @var array<int, string|Thing|DataTypes\DataType>
761
        */
762 2
        $value = array_values(array_filter(
763 2
            array_map(
764
                /**
765
                * @param string|Thing|DataTypes\DataType $val
766
                *
767
                * @return string|Thing|DataTypes\DataType
768
                */
769
                function ($val) {
770 1
                    return is_string($val) ? trim($val) : $val;
771 2
                },
772 2
                $value
773
            ),
774
            /**
775
            * @param string|Thing|DataTypes\DataType $maybe
776
            */
777
            function ($maybe) : bool {
778 1
                return '' !== $maybe;
779 2
            }
780
        ));
781
782 2
        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 2
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
792 2
    }
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 18
    protected function NudgePropertyWithUniqueTrimmedStringsMightNotBeString(
847
        string $property,
848
        string $method,
849
        array $value
850
    ) : void {
851 18
        $initialCount = count($value);
852
853
        $value = array_filter(array_map('trim', $value), function (string $maybe) : bool {
854 17
            return '' !== $maybe;
855 18
        });
856
857 18
        if ($initialCount !== count($value)) {
858
            throw new InvalidArgumentException(
859
                'Arguments passed to ' .
860
                $method .
861
                ' must not have trailing whitespace!'
862
            );
863
        }
864
865 18
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
866 18
    }
867
868
    /**
869
    * @param array<int, int|float|string> $value
870
    */
871
    protected function NudgePropertyWithUniqueTrimmedStringsNumericsMightNotBeStringsOrNumerics(
872
        string $property,
873
        string $method,
874
        array $value
875
    ) : void {
876
        $initialCount = count($value);
877
878
        /**
879
        * @var array<int, string|float|int>
880
        */
881
        $value = array_filter(
882
            $value,
883
            /**
884
            * @param mixed $maybe
885
            */
886
            function ($maybe) : bool {
887
                return is_string($maybe) || is_numeric($maybe);
888
            }
889
        );
890
891
        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
        $value = array_unique(
903
            array_map(
904
                /**
905
                * @param string|float|int $val
906
                *
907
                * @return string|float|int
908
                */
909
                function ($val) {
910
                    return
911
                        is_string($val)
912
                            ? (
913
                                ctype_digit($val)
914
                                    ? (int) $val
915
                                    : (
916
                                        is_numeric($val)
917
                                            ? (float) $val
918
                                            : trim($val)
919
                                    )
920
                            )
921
                            : $val;
922
                },
923
                $value
924
            ),
925
            SORT_NUMERIC
926
        );
927
928
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
929
    }
930
931
    /**
932
    * @param array<int, int> $value
933
    */
934 10
    protected function NudgePropertyWithUniqueIntegers(
935
        string $property,
936
        string $method,
937
        array $value
938
    ) : void {
939 10
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
940 10
    }
941
942
    /**
943
    * @param array<int, int|float|string> $value
944
    */
945 3
    protected function NudgePropertyWithUniqueIntegersOrFloats(
946
        string $property,
947
        string $method,
948
        array $value
949
    ) : void {
950 3
        $initialCount = count($value);
951
952
        /**
953
        * @var array<int, int|float>
954
        */
955 3
        $value = array_map(
956
            /**
957
            * @param int|float|string $val
958
            *
959
            * @return int|float
960
            */
961
            function ($val) {
962
                return
963 3
                    (is_int($val) || is_float($val))
964 3
                        ? $val
965
                        : (
966
                            ctype_digit($val)
967
                                ? (int) $val
968 3
                                : (float) $val
969
                        );
970 3
            },
971 3
            array_filter($value, 'is_numeric')
972
        );
973
974 3
        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 3
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
983 3
    }
984
985
    /**
986
    * @param array<int, bool> $value
987
    */
988 3
    protected function NudgePropertyWithUniqueBooleans(
989
        string $property,
990
        array $value
991
    ) : void {
992 3
        $replace = [];
993 3
        if (in_array(true, $value, true)) {
994 3
            $replace[] = true;
995
        }
996 3
        if (in_array(false, $value, true)) {
997
            $replace[] = false;
998
        }
999
1000 3
        $this->NudgePropertyValue($property, $replace);
1001 3
    }
1002
1003
    /**
1004
    * @param array<int, int|string> $value
1005
    */
1006
    protected function NudgePropertWithUniqueIntegersOrTrimmedStrings(
1007
        string $property,
1008
        string $method,
1009
        array $value
1010
    ) : void {
1011
        $initialCount = count($value);
1012
1013
        $value = array_map(
1014
            /**
1015
            * @param int|string $val
1016
            *
1017
            * @return int|string
1018
            */
1019
            function ($val) {
1020
                return is_string($val) ? trim($val) : $val;
1021
            },
1022
            array_filter(
1023
                $value,
1024
                /**
1025
                * @param mixed $maybe
1026
                */
1027
                function ($maybe) : bool {
1028
                    return is_string($maybe) || is_int($maybe);
1029
                }
1030
            )
1031
        );
1032
1033
        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
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
1042
    }
1043
1044
    /**
1045
    * @param array<int, scalar|array|object|null> $value
1046
    */
1047 34
    protected function NudgePropertyWithUniqueValues(
1048
        string $property,
1049
        string $method,
1050
        array $value,
1051
        int $sort_flags = SORT_REGULAR
1052
    ) : void {
1053 34
        $initialCount = count($value);
1054
1055 34
        $value = array_values(array_unique($value, $sort_flags));
1056
1057 34
        if ($initialCount !== count($value)) {
1058
            throw new InvalidArgumentException(
1059
                'Arguments passed to ' .
1060
                $method .
1061
                ' must be unique!'
1062
            );
1063
        }
1064
1065 34
        $this->NudgePropertyValue($property, $value);
1066 34
    }
1067
1068
    /**
1069
    * @param array<int, Date> $value
1070
    */
1071
    protected function NudgePropertyWithUniqueDates(
1072
        string $property,
1073
        string $method,
1074
        array $value
1075
    ) : void {
1076
        $this->NudgePropertyWithUniqueValuesOfThings($property, $method, $value, Date::class);
1077
    }
1078
1079
    /**
1080
    * @param array<int, DateTime> $value
1081
    */
1082
    protected function NudgePropertyWithUniqueDateTimes(
1083
        string $property,
1084
        string $method,
1085
        array $value
1086
    ) : void {
1087
        $this->NudgePropertyWithUniqueValuesOfThings($property, $method, $value, DateTime::class);
1088
    }
1089
1090
    /**
1091
    * @param array<int, Date|DateTime> $value
1092
    */
1093
    protected function NudgePropertyWithUniqueDatesOrDateTimes(
1094
        string $property,
1095
        string $method,
1096
        array $value
1097
    ) : void {
1098
        $this->NudgePropertyWithUniqueValuesOfThings(
1099
            $property,
1100
            $method,
1101
            $value,
1102
            Date::class,
1103
            DateTime::class
1104
        );
1105
    }
1106
1107
    /**
1108
    * @param array<int, Person> $value
1109
    */
1110
    protected function NudgePropertyWithUniquePersons(
1111
        string $property,
1112
        string $method,
1113
        array $value
1114
    ) : void {
1115
        $this->NudgePropertyWithUniqueValuesOfThings(
1116
            $property,
1117
            $method,
1118
            $value,
1119
            Person::class
1120
        );
1121
    }
1122
1123
    /**
1124
    * @param array<int, Organization|Person> $value
1125
    */
1126 3
    protected function NudgePropertyWithUniqueOrganizationsOrPersons(
1127
        string $property,
1128
        string $method,
1129
        array $value
1130
    ) : void {
1131 3
        $this->NudgePropertyWithUniqueValuesOfThings(
1132 3
            $property,
1133 3
            $method,
1134 3
            $value,
1135 3
            Organization::class,
1136 3
            Person::class
1137
        );
1138 3
    }
1139
1140
    /**
1141
    * @param array<int, Organization> $value
1142
    */
1143
    protected function NudgePropertyWithUniqueOrganizations(
1144
        string $property,
1145
        string $method,
1146
        array $value
1147
    ) : void {
1148
        $this->NudgePropertyWithUniqueValuesOfThings(
1149
            $property,
1150
            $method,
1151
            $value,
1152
            Organization::class
1153
        );
1154
    }
1155
1156
    /**
1157
    * @param array<int, Event> $value
1158
    */
1159
    protected function NudgePropertyWithUniqueEvents(
1160
        string $property,
1161
        string $method,
1162
        array $value
1163
    ) : void {
1164
        $this->NudgePropertyWithUniqueValuesOfThings(
1165
            $property,
1166
            $method,
1167
            $value,
1168
            Event::class
1169
        );
1170
    }
1171
1172
    /**
1173
    * @param array<int, Place> $value
1174
    */
1175
    protected function NudgePropertyValueWithUniquePlaces(
1176
        string $property,
1177
        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

1177
        /** @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...
1178
        array $value
1179
    ) : void {
1180
        $this->NudgePropertyWithUniqueValuesOfThings(
1181
            $property,
1182
            __METHOD__,
1183
            $value,
1184
            Place::class
1185
        );
1186
    }
1187
1188
    /**
1189
    * @param array<int, ContactPoint|Place> $value
1190
    */
1191
    protected function NudgePropertyValueWithUniqueContactPointsOrPlaces(
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
        $this->NudgePropertyWithUniqueValuesOfThings(
1197
            $property,
1198
            __METHOD__,
1199
            $value,
1200
            ContactPoint::class,
1201
            Place::class
1202
        );
1203
    }
1204
1205
    /**
1206
    * @param array<int, Thing|DataTypes\DataType> $value
1207
    *
1208
    * @psalm-param class-string<Thing>|class-string<DataTypes\DataType> $validThing
1209
    * @psalm-param class-string<Thing>|class-string<DataTypes\DataType> ...$validThings
1210
    */
1211 16
    protected function NudgePropertyWithUniqueValuesOfThings(
1212
        string $property,
1213
        string $method,
1214
        array $value,
1215
        string $validThing,
1216
        string ...$validThings
1217
    ) : void {
1218 16
        array_unshift($validThings, $validThing);
1219
1220 16
        $initialCount = count($validThings);
1221
1222 16
        if (count($validThings) !== $initialCount) {
1223
            throw new InvalidArgumentException(
1224
                'Arguments 4+ passed to ' .
1225
                __METHOD__ .
1226
                ' must be implementations of ' .
1227
                Thing::class .
1228
                '!'
1229
            );
1230
        }
1231
1232 16
        $initialCount = count($value);
1233
1234
        /**
1235
        * @var array<int, Thing|DataTypes\DataType>
1236
        */
1237 16
        $value = array_filter(
1238 16
            $value,
1239
            /**
1240
            * @param Thing|DataTypes\DataType $maybe
1241
            */
1242
            function ($maybe) use ($validThings) : bool {
1243 15
                foreach ($validThings as $thing) {
1244 15
                    if (is_a($maybe, $thing, true)) {
1245 15
                        return true;
1246
                    }
1247
                }
1248
1249
                return false;
1250 16
            }
1251
        );
1252
1253 16
        if (count($value) !== $initialCount) {
1254
            throw new InvalidArgumentException(
1255
                'Argument 1 passed to ' .
1256
                $method .
1257
                ' must be an array containing any combination of ' .
1258
                implode(', ', $validThings)
1259
            );
1260
        }
1261
1262 16
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
1263 16
    }
1264
}
1265