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

rotexsoft / versatile-collections / 6042672854

31 Aug 2023 09:32PM UTC coverage: 98.445% (-0.09%) from 98.532%
6042672854

push

github

rotimi
Upgraded rector to 0.18

1076 of 1093 relevant lines covered (98.44%)

28.94 hits per line

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

94.44
/src/SpecificObjectsCollection.php
1
<?php /** @noinspection PhpFullyQualifiedNameUsageInspection */
2
declare(strict_types=1);
3
namespace VersatileCollections;
4

5
/**
6
 * Description of SpecificObjectsCollection
7
 *  
8
 * Below is a list of acceptable value(s), that could be comma separated, 
9
 * for the @used-for tag in phpdoc blocks for public methods in this class:
10
 *  
11
 *      - accessing-or-extracting-keys-or-items
12
 *      - adding-items
13
 *      - adding-methods-at-runtime
14
 *      - checking-keys-presence
15
 *      - checking-items-presence
16
 *      - creating-new-collections
17
 *      - deleting-items
18
 *      - finding-or-searching-for-items
19
 *      - getting-collection-meta-data
20
 *      - iteration
21
 *      - mathematical-operations
22
 *      - modifying-keys
23
 *      - modifying-items
24
 *      - ordering-or-sorting-items
25
 *      - other-operations
26
 *
27
 */
28
final class SpecificObjectsCollection extends ObjectsCollection {
29

30
    private ?string $class_name = null;
31

32
    /** @noinspection PhpMissingParentConstructorInspection
33
     * @noinspection PhpUnnecessaryStaticReferenceInspection
34
     * @noRector \Rector\DeadCode\Rector\ClassMethod\RemoveUselessParamTagRector
35
     */
36
    protected function __construct(object ...$objects) {
37
        
38
        if( \is_null($this->class_name) ) {
54✔
39
            
40
            // we don't have a specific class, allow all objects
41
            $this->versatile_collections_items = $objects;
54✔
42
            
43
        } else {
44
            
45
            // we have a specific class, allow only instances of that class
46
            // use the strictly typed constructor instead to enforce the
47
            // strict typing
48
            static::strictlyTypedCollectionTrait__construct(...$objects);
×
49
        }
50
    }
51

52
    /**
53
     * Create a new collection that only stores instances of the specified fully qualified class name or
54
     * its sub-classes or a new collection that stores any kind of object if no fully qualified class name
55
     * was specified (Essentially works like ObjectsCollection in the latter case).
56
     *
57
     * @param string|null $class_name fully qualified name of the class whose instances or instances of its sub-classes alone would be stored in the collection.
58
     *                                Set it to null to make the collection work exactly like an instance of ObjectsCollection
59
     * @param iterable $items an iterable of objects to be stored in the new collection
60
     * @param bool $preserve_keys true to use the same keys in $items in the collection, , else false to use sequentially incrementing numeric keys starting from zero
61
     * 
62
     * 
63
     * @used-for: creating-new-collections
64
     * 
65
     * @title: Create a new collection that only stores instances of the specified fully qualified class name or its sub-classes or a new collection that stores any kind of object if no fully qualified class name was specified (Essentially works like ObjectsCollection in the latter case).
66
     *
67
     * @noinspection PhpUnnecessaryStaticReferenceInspection
68
     * @psalm-suppress LessSpecificReturnStatement
69
     * @psalm-suppress MoreSpecificReturnType
70
     * @psalm-suppress NoInterfaceProperties
71
     */
72
    public static function makeNewForSpecifiedClassName(?string $class_name=null, iterable $items =[], bool $preserve_keys=true): StrictlyTypedCollectionInterface
73
    {
74
        
75
        if( $class_name === null ) {
54✔
76

77
            return static::makeNew($items, $preserve_keys); // collection that stores any type of object
30✔
78
        }
79
        
80
        // Class was specified, create collection for only instances of the specified class
81
        $new_collection = static::makeNew(); // make an empty collection first
36✔
82
        $new_collection->class_name = $class_name;
36✔
83

84
        foreach ($items as $key => $val) {
36✔
85

86
            if ($preserve_keys) {
30✔
87

88
                $new_collection[$key] = $val;
30✔
89

90
            } else {
91

92
                $new_collection[] = $val;
6✔
93
            }
94
        }
95
        
96
        return $new_collection;
18✔
97
    }
98

99
    /**
100
     *
101
     * @param mixed $item
102
     * @return bool true if $item is of the expected type, else false
103
     */
104
    public function checkType($item): bool {
105
        
106
        return \is_null($this->class_name)
48✔
107
                ? parent::checkType($item)
24✔
108
                : ($item instanceof $this->class_name);
48✔
109
    }
110
    
111
    public function getTypes(): StringsCollection {
112
        
113
        return \is_null($this->class_name)
30✔
114
                ? parent::getTypes()
12✔
115
                : new StringsCollection($this->class_name);
30✔
116
    }
117
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc