Passed
Branch fuzzy-generators (a45711)
by SignpostMarv
09:29
created

Thing   F

Complexity

Total Complexity 98

Size/Duplication

Total Lines 1234
Duplicated Lines 0 %

Test Coverage

Coverage 67.66%

Importance

Changes 0
Metric Value
eloc 418
dl 0
loc 1234
ccs 295
cts 436
cp 0.6766
rs 2
c 0
b 0
f 0
wmc 98

58 Methods

Rating   Name   Duplication   Size   Complexity  
A NudgePropertyWithUniqueDates() 0 6 1
A NudgePropertyWithUniqueOrganizations() 0 10 1
A NudgePropertyValueWithUniqueContactPointsOrPlaces() 0 11 1
A NudgePropertyWithUniqueDateTimes() 0 6 1
A NudgePropertyValueWithUniquePlaces() 0 10 1
A NudgePropertyWithUniqueEvents() 0 10 1
A NudgePropertyWithUniquePersons() 0 10 1
A NudgePropertyWithUniqueDatesOrDateTimes() 0 11 1
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 NudgePropertWithUniqueIntegersOrTrimmedStrings() 0 36 4
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
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 GetIdentifier() 0 12 1
A NudgePropertyWithUniqueBooleansOrThings() 0 7 1
A SetAlternateName() 0 6 1
A DaftObjectPropertiesWithMultiTypedArraysOfUniqueValues() 0 23 2
A NudgePropertyWithUniqueTrimmedStringsOrThings() 0 41 3
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 NudgePropertyWithUniqueValuesOfThings() 0 52 5
A NudgePropertyWithUniqueOrganizationsOrPersons() 0 11 1
A NudgePropertyWithUniqueValues() 0 27 4

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 2594
    public function __construct(array $data = [], bool $writeAll = false)
106
    {
107 2594
        $missing = array_diff(static::DaftObjectProperties(), array_keys($data));
108
109
        /**
110
        * @var array<int, string>
111
        */
112 2594
        $missing = array_combine(
113 2594
            $missing,
114 2594
            array_fill(0, count($missing), [])
115
        );
116
117
        /**
118
        * @var array<string, scalar|array|object|null>
119
        */
120 2594
        $data = array_merge(
121 2594
            $data,
122 2594
            $missing
123
        );
124
125 2594
        unset($data['@context'], $data['@type']);
126
127 2594
        parent::__construct($data, $writeAll);
128 2594
    }
129
130 67
    public function ObtainContext() : string
131
    {
132 67
        return (string) static::SCHEMA_ORG_CONTEXT;
133
    }
134
135 67
    public function ObtainType() : string
136
    {
137 67
        return (string) static::SCHEMA_ORG_TYPE;
138
    }
139
140
    /**
141
    * @return array<int, string>
142
    */
143 67
    public function GetAdditionalType() : array
144
    {
145
        /**
146
        * @var array<int, string>
147
        */
148 67
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
149 67
            'additionalType',
150 67
            $this->RetrievePropertyValueFromData('additionalType'),
151 67
            static::class
152
        );
153
154 67
        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 67
    public function GetAlternateName() : array
173
    {
174
        /**
175
        * @var array<int, string>
176
        */
177 67
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
178 67
            'alternateName',
179 67
            $this->RetrievePropertyValueFromData('alternateName'),
180 67
            static::class
181
        );
182
183 67
        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 67
    public function GetDescription() : array
202
    {
203
        /**
204
        * @var array<int, string>
205
        */
206 67
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
207 67
            'description',
208 67
            $this->RetrievePropertyValueFromData('description'),
209 67
            static::class
210
        );
211
212 67
        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 67
    public function GetDisambiguatingDescription() : array
231
    {
232
        /**
233
        * @var array<int, string>
234
        */
235 67
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
236 67
            'disambiguatingDescription',
237 67
            $this->RetrievePropertyValueFromData('disambiguatingDescription'),
238 67
            static::class
239
        );
240
241 67
        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 70
    public function GetIdentifier() : array
260
    {
261
        /**
262
        * @var array<int, string|PropertyValue>
263
        */
264 70
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
265 70
            'identifier',
266 70
            $this->RetrievePropertyValueFromData('identifier'),
267 70
            static::class
268
        );
269
270 70
        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 67
    public function GetImage() : array
289
    {
290
        /**
291
        * @var array<int, string|ImageObject>
292
        */
293 67
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
294 67
            'image',
295 67
            $this->RetrievePropertyValueFromData('image'),
296 67
            static::class
297
        );
298
299 67
        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 67
    public function GetMainEntityOfPage() : array
319
    {
320
        /**
321
        * @var array<int, string|CreativeWork>
322
        */
323 67
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
324 67
            'mainEntityOfPage',
325 67
            $this->RetrievePropertyValueFromData('mainEntityOfPage'),
326 67
            static::class
327
        );
328
329 67
        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 79
    public function GetName() : array
349
    {
350
        /**
351
        * @var array<int, string>
352
        */
353 79
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
354 79
            'name',
355 79
            $this->RetrievePropertyValueFromData('name'),
356 79
            static::class
357
        );
358
359 79
        return $out;
360
    }
361
362
    /**
363
    * @param array<int, string> $value
364
    */
365 13
    public function SetName(array $value) : void
366
    {
367 13
        $this->NudgePropertyWithUniqueTrimmedStringsMightNotBeString('name', __METHOD__, $value);
368 13
    }
369
370
    /**
371
    * @return array<int, Action>
372
    */
373 67
    public function GetPotentialAction() : array
374
    {
375
        /**
376
        * @var array<int, Action>
377
        */
378 67
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
379 67
            'potentialAction',
380 67
            $this->RetrievePropertyValueFromData('potentialAction'),
381 67
            static::class
382
        );
383
384 67
        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 67
    public function GetSameAs() : array
404
    {
405
        /**
406
        * @var array<int, string>
407
        */
408 67
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
409 67
            'sameAs',
410 67
            $this->RetrievePropertyValueFromData('sameAs'),
411 67
            static::class
412
        );
413
414 67
        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 67
    public function GetSubjectOf() : array
429
    {
430
        /**
431
        * @var array<int, CreativeWork|Event>
432
        */
433 67
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
434 67
            'subjectOf',
435 67
            $this->RetrievePropertyValueFromData('subjectOf'),
436 67
            static::class
437
        );
438
439 67
        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 67
    public function GetUrl() : array
454
    {
455
        /**
456
        * @var array<int, string>
457
        */
458 67
        $out = TypeUtilities::ExpectRetrievedValueIsArray(
459 67
            'url',
460 67
            $this->RetrievePropertyValueFromData('url'),
461 67
            static::class
462
        );
463
464 67
        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 67
    public function jsonSerialize() : array
476
    {
477 67
        return array_filter(
478 67
            parent::jsonSerialize(),
479
            /**
480
            * @param scalar|array|object|null $val
481
            */
482
            function ($val) : bool {
483 67
                return ! is_array($val) || count($val) > 0;
484 67
            }
485
        );
486
    }
487
488 13030
    public static function DaftObjectProperties() : array
489
    {
490
        /**
491
        * @var array<int, string>
492
        */
493 13030
        $static = static::PROPERTIES;
494
495
        /**
496
        * @var string
497
        *
498
        * @psalm-var class-string<Thing>
499
        */
500 13030
        $static_parent = get_parent_class(static::class);
501
502 13030
        $parent = $static_parent::DaftObjectProperties();
503
504 13030
        return array_unique(array_merge(
505 13030
            $parent,
506 13030
            $static
507
        ));
508
    }
509
510
    /**
511
    * @return array<string, array<int, string>>
512
    */
513 133
    public static function DaftObjectPropertiesWithMultiTypedArraysOfUniqueValues() : array
514
    {
515
        /**
516
        * @var array<string, array<int, string>>
517
        */
518 133
        $static = static::PROPERTIES_WITH_MULTI_TYPED_ARRAYS;
519
520
        /**
521
        * @var string
522
        *
523
        * @psalm-var class-string<Thing>
524
        */
525 133
        $static_parent = get_parent_class(static::class);
526
527 133
        if ($static_parent === get_parent_class(self::class)) {
528 133
            return $static;
529
        }
530
531 133
        $parent = $static_parent::DaftObjectPropertiesWithMultiTypedArraysOfUniqueValues();
532
533 133
        return array_merge(
534 133
            $parent,
535 133
            $static
536
        );
537
    }
538
539 2706
    public static function DaftObjectNullableProperties() : array
540
    {
541
        /**
542
        * @var array<int, string>
543
        */
544 2706
        $static = static::NULLABLE_PROPERTIES;
545
546
        /**
547
        * @var string
548
        *
549
        * @psalm-var class-string<Thing>
550
        */
551 2706
        $static_parent = get_parent_class(static::class);
552
553 2706
        $parent = $static_parent::DaftObjectNullableProperties();
554
555 2706
        return array_unique(array_merge(
556 2706
            $parent,
557 2706
            $static
558
        ));
559
    }
560
561 290
    public static function DaftObjectExportableProperties() : array
562
    {
563 290
        return static::DaftObjectProperties();
564
    }
565
566 133
    public static function DaftObjectJsonProperties() : array
567
    {
568 133
        return static::DaftObjectProperties();
569
    }
570
571 133
    public static function DaftObjectPublicGetters() : array
572
    {
573
        /**
574
        * @var string
575
        *
576
        * @psalm-var class-string<Thing>
577
        */
578 133
        $static_parent = get_parent_class(static::class);
579
580 133
        $static = TypeUtilities::DaftObjectPublicGetters(static::class);
581
582 133
        if ($static_parent === get_parent_class(self::class)) {
583 1
            return $static;
584
        }
585
586 133
        return array_unique(array_merge(
587 133
            TypeUtilities::DaftObjectPublicGetters($static_parent),
588 133
            $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 67
    public static function DaftObjectPublicSetters() : array
614
    {
615
        /**
616
        * @var string
617
        *
618
        * @psalm-var class-string<Thing>
619
        */
620 67
        $static_parent = get_parent_class(static::class);
621
622 67
        $static = TypeUtilities::DaftObjectPublicSetters(static::class);
623
624 67
        if ($static_parent === get_parent_class(self::class)) {
625 1
            return $static;
626
        }
627
628 67
        return array_unique(array_merge(
629 67
            TypeUtilities::DaftObjectPublicSetters($static_parent),
630 67
            $static
631
        ));
632
    }
633
634
    /**
635
    * @return static
636
    *
637
    * @psalm-return Thing
638
    */
639 67
    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 67
        $decoded = json_decode($string, true);
647
648
        /**
649
        * @var static
650
        */
651 67
        $out = static::DaftObjectFromJsonArray($decoded, $writeAll);
652
653 67
        return $out;
654
    }
655
656
    /**
657
    * @return static
658
    *
659
    * @psalm-return Thing
660
    */
661 67
    public static function DaftObjectFromJsonArray(
662
        array $array,
663
        bool $writeAll = self::BOOL_DEFAULT_WRITEALL
664
    ) : DaftJson {
665 67
        $type = JsonTypeUtilities::ThrowIfNotDaftJson(static::class);
666
667 67
        $multi_type = static::DaftObjectPropertiesWithMultiTypedArraysOfUniqueValues();
668
669 67
        $data = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
670
671 67
        $array_keys = array_keys($array);
672
673 67
        $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 67
                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 67
                if (is_array($array[$k])) {
689 67
                    return array_map(
690
                        /**
691
                        * @param mixed $val
692
                        *
693
                        * @return mixed
694
                        */
695
                        function ($val) use ($k, $multi_type) {
696
                            if (
697 67
                                is_array($val) &&
698 67
                                isset($val['@context'], $val['@type'], $multi_type[$k])
699
                            ) {
700 53
                                foreach ($multi_type[$k] as $maybe) {
701
                                    if (
702 53
                                        is_a($maybe, Thing::class, true) &&
703 53
                                        $val['@context'] === $maybe::SCHEMA_ORG_CONTEXT &&
704 53
                                        $val['@type'] === $maybe::SCHEMA_ORG_TYPE
705
                                    ) {
706 53
                                        return $maybe::DaftObjectFromJsonArray($val);
707
                                    }
708
                                }
709
                            }
710
711 67
                            return $val;
712 67
                        },
713 67
                        $array[$k]
714
                    );
715
                }
716
717 67
                return $array[$k];
718 67
            },
719 67
            $array_keys
720
        ));
721
722
        /**
723
        * @psalm-var Thing
724
        *
725
        * @var Thing
726
        */
727 67
        $out = new $type($data, $writeAll);
728
729 67
        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 1
    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 1
        $initialCount = count($value);
758
759
        /**
760
        * @var array<int, string|Thing|DataTypes\DataType>
761
        */
762 1
        $value = array_values(array_filter(
763 1
            array_map(
764
                /**
765
                * @param string|Thing|DataTypes\DataType $val
766
                *
767
                * @return string|Thing|DataTypes\DataType
768
                */
769
                function ($val) {
770
                    return is_string($val) ? trim($val) : $val;
771 1
                },
772 1
                $value
773
            ),
774
            /**
775
            * @param string|Thing|DataTypes\DataType $maybe
776
            */
777
            function ($maybe) : bool {
778
                return '' !== $maybe;
779 1
            }
780
        ));
781
782 1
        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 1
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
792 1
    }
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 19
    protected function NudgePropertyWithUniqueTrimmedStringsMightNotBeString(
847
        string $property,
848
        string $method,
849
        array $value
850
    ) : void {
851 19
        $initialCount = count($value);
852
853
        $value = array_filter(array_map('trim', $value), function (string $maybe) : bool {
854 18
            return '' !== $maybe;
855 19
        });
856
857 19
        if ($initialCount !== count($value)) {
858
            throw new InvalidArgumentException(
859
                'Arguments passed to ' .
860
                $method .
861
                ' must not have trailing whitespace!'
862
            );
863
        }
864
865 19
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
866 19
    }
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 16
    protected function NudgePropertyWithUniqueIntegers(
935
        string $property,
936
        string $method,
937
        array $value
938
    ) : void {
939 16
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
940 16
    }
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 15
    protected function NudgePropertyWithUniqueBooleans(
989
        string $property,
990
        array $value
991
    ) : void {
992 15
        $replace = [];
993 15
        if (in_array(true, $value, true)) {
994 15
            $replace[] = true;
995
        }
996 15
        if (in_array(false, $value, true)) {
997
            $replace[] = false;
998
        }
999
1000 15
        $this->NudgePropertyValue($property, $replace);
1001 15
    }
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 64
    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 64
        $initialCount = count($value);
1054
1055 64
        $out = [];
1056
1057 64
        foreach ($value as $val) {
1058 64
            if ( ! in_array($val, $out, true)) {
1059 64
                $out[] = $val;
1060
            }
1061
        }
1062
1063 64
        $value = $out;
1064
1065 64
        if ($initialCount !== count($value)) {
1066
            throw new InvalidArgumentException(
1067
                'Arguments passed to ' .
1068
                $method .
1069
                ' must be unique!'
1070
            );
1071
        }
1072
1073 64
        $this->NudgePropertyValue($property, $value);
1074 64
    }
1075
1076
    /**
1077
    * @param array<int, Date> $value
1078
    */
1079
    protected function NudgePropertyWithUniqueDates(
1080
        string $property,
1081
        string $method,
1082
        array $value
1083
    ) : void {
1084
        $this->NudgePropertyWithUniqueValuesOfThings($property, $method, $value, Date::class);
1085
    }
1086
1087
    /**
1088
    * @param array<int, DateTime> $value
1089
    */
1090
    protected function NudgePropertyWithUniqueDateTimes(
1091
        string $property,
1092
        string $method,
1093
        array $value
1094
    ) : void {
1095
        $this->NudgePropertyWithUniqueValuesOfThings($property, $method, $value, DateTime::class);
1096
    }
1097
1098
    /**
1099
    * @param array<int, Date|DateTime> $value
1100
    */
1101
    protected function NudgePropertyWithUniqueDatesOrDateTimes(
1102
        string $property,
1103
        string $method,
1104
        array $value
1105
    ) : void {
1106
        $this->NudgePropertyWithUniqueValuesOfThings(
1107
            $property,
1108
            $method,
1109
            $value,
1110
            Date::class,
1111
            DateTime::class
1112
        );
1113
    }
1114
1115
    /**
1116
    * @param array<int, Person> $value
1117
    */
1118
    protected function NudgePropertyWithUniquePersons(
1119
        string $property,
1120
        string $method,
1121
        array $value
1122
    ) : void {
1123
        $this->NudgePropertyWithUniqueValuesOfThings(
1124
            $property,
1125
            $method,
1126
            $value,
1127
            Person::class
1128
        );
1129
    }
1130
1131
    /**
1132
    * @param array<int, Organization|Person> $value
1133
    */
1134 46
    protected function NudgePropertyWithUniqueOrganizationsOrPersons(
1135
        string $property,
1136
        string $method,
1137
        array $value
1138
    ) : void {
1139 46
        $this->NudgePropertyWithUniqueValuesOfThings(
1140 46
            $property,
1141 46
            $method,
1142 46
            $value,
1143 46
            Organization::class,
1144 46
            Person::class
1145
        );
1146 46
    }
1147
1148
    /**
1149
    * @param array<int, Organization> $value
1150
    */
1151
    protected function NudgePropertyWithUniqueOrganizations(
1152
        string $property,
1153
        string $method,
1154
        array $value
1155
    ) : void {
1156
        $this->NudgePropertyWithUniqueValuesOfThings(
1157
            $property,
1158
            $method,
1159
            $value,
1160
            Organization::class
1161
        );
1162
    }
1163
1164
    /**
1165
    * @param array<int, Event> $value
1166
    */
1167
    protected function NudgePropertyWithUniqueEvents(
1168
        string $property,
1169
        string $method,
1170
        array $value
1171
    ) : void {
1172
        $this->NudgePropertyWithUniqueValuesOfThings(
1173
            $property,
1174
            $method,
1175
            $value,
1176
            Event::class
1177
        );
1178
    }
1179
1180
    /**
1181
    * @param array<int, Place> $value
1182
    */
1183
    protected function NudgePropertyValueWithUniquePlaces(
1184
        string $property,
1185
        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

1185
        /** @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...
1186
        array $value
1187
    ) : void {
1188
        $this->NudgePropertyWithUniqueValuesOfThings(
1189
            $property,
1190
            __METHOD__,
1191
            $value,
1192
            Place::class
1193
        );
1194
    }
1195
1196
    /**
1197
    * @param array<int, ContactPoint|Place> $value
1198
    */
1199
    protected function NudgePropertyValueWithUniqueContactPointsOrPlaces(
1200
        string $property,
1201
        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

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