Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
39 / 39
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
TreeDefinitions
100.00% covered (success)
100.00%
39 / 39
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 makeDefinitions
100.00% covered (success)
100.00%
39 / 39
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace pvc\struct\tree\di;
6
7use League\Container\Definition\Definition;
8use League\Container\Definition\DefinitionInterface;
9use pvc\interfaces\struct\collection\CollectionInterface;
10use pvc\interfaces\struct\collection\CollectionOrderedByIndexFactoryInterface;
11use pvc\interfaces\struct\collection\CollectionOrderedByIndexInterface;
12use pvc\interfaces\struct\collection\CollectionOrderedFactoryInterface;
13use pvc\interfaces\struct\collection\CollectionOrderedInterface;
14use pvc\interfaces\struct\tree\node\TreenodeChildCollectionFactoryInterface;
15use pvc\interfaces\struct\tree\node\TreenodeChildCollectionInterface;
16use pvc\interfaces\struct\tree\node\TreenodeFactoryInterface;
17use pvc\interfaces\struct\tree\node\TreenodeInterface;
18use pvc\interfaces\struct\tree\tree\TreeInterface;
19use pvc\interfaces\struct\tree\tree\TreenodeCollectionInterface;
20use pvc\struct\collection\Collection;
21use pvc\struct\collection\CollectionOrderedByIndex;
22use pvc\struct\collection\CollectionOrderedByIndexFactory;
23use pvc\struct\tree\node\Treenode;
24use pvc\struct\tree\node\TreenodeChildCollection;
25use pvc\struct\tree\node\TreenodeChildCollectionFactory;
26use pvc\struct\tree\node\TreenodeFactory;
27use pvc\struct\tree\tree\Tree;
28use pvc\struct\tree\tree\TreenodeCollection;
29
30class TreeDefinitions
31{
32    /**
33     * @return array<int, DefinitionInterface>
34     */
35    public static function makeDefinitions(): array
36    {
37        return [
38            /**
39             * map interfaces to implementations
40             */
41
42            new Definition(CollectionInterface::class, Collection::class),
43
44            new Definition(CollectionOrderedByIndexInterface::class, CollectionOrderedByIndex::class),
45            new Definition(CollectionOrderedByIndexFactoryInterface::class, CollectionOrderedByIndexFactory::class),
46
47            new Definition(TreenodeChildCollectionInterface::class, TreenodeChildCollection::class),
48            new Definition(TreenodeChildCollectionFactoryInterface::class, TreenodeChildCollectionFactory::class),
49
50            new Definition(TreenodeInterface::class, Treenode::class),
51            new Definition(TreenodeFactoryInterface::class, TreenodeFactory::class),
52            new Definition(TreenodeCollectionInterface::class, TreenodeCollection::class),
53
54            new Definition(TreeInterface::class, Tree::class),
55
56            /**
57             * definitions for the implementations
58             */
59            new Definition(Collection::class),
60            new Definition(CollectionOrderedByIndex::class),
61            new Definition(CollectionOrderedByIndexFactory::class)
62                ->setShared(),
63
64            new Definition(TreenodeChildCollection::class),
65            new Definition(TreenodeChildCollectionFactory::class)
66                ->setShared(),
67
68            new Definition(Treenode::class, Treenode::class)
69                ->addArgument(TreenodeChildCollectionFactoryInterface::class),
70
71            new Definition(TreenodeFactory::class)
72                ->addArgument(TreenodeChildCollectionFactoryInterface::class)
73                /**
74                 * why is the argument to setShared required here and not elsewhere
75                 * (phpstan picks it up)
76                 */
77                ->setShared(true),
78
79            new Definition(TreenodeCollection::class),
80
81            new Definition(Tree::class)
82                /**
83                 * should be able to type hint these as interfaces, but I think there's
84                 * a bug in the League's resolver code.  Type hinting the interface
85                 * resolves down to the concrete classes, but *without* any arguments
86                 * that are part of the definitions.  in other words, there is a call to
87                 * $container->get(TreenodeFactory::class) but it dies because it can't find
88                 * the argument for the constructor which is defined above.
89                 */
90                ->addArgument(TreenodeFactory::class)
91                ->addArgument(TreenodeCollection::class),
92        ];
93    }
94}