CollectionsReader   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 6
c 4
b 0
f 2
lcom 1
cbo 3
dl 0
loc 55
ccs 31
cts 31
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B getCollectionsItemNames() 0 40 6
1
<?php
2
3
namespace Accessible\Reader;
4
5
use Doctrine\Common\Inflector\Inflector;
6
7
class CollectionsReader extends Reader
8
{
9
    /**
10
     * Get a list linking an item name with the property it refers to and what kind of collection it is.
11
     * Ex: [
12
     *   "byItemName" => "user" => ["property" => "users", "behavior" => "list", "methods" => ["add", "remove"]],
13
     *   "byProperty" => "users" => ["itemName" => "user", "behavior" => "list", "methods" => ["add", "remove"]]
14
     * ]
15
     *
16
     * @param array  $properties The properties of the object to read.
17
     * @param \Doctrine\Common\Annotations\Reader $annotationReader The annotation reader to use.
18
     *
19
     * @return array The described list.
20
     */
21 7
    public static function getCollectionsItemNames($properties, $annotationReader)
22
    {
23
        $objectCollectionsItemNames = array(
24 7
            "byProperty" => array(),
25 7
            "byItemName" => array()
26 7
        );
27
28 7
        foreach ($properties as $propertyName => $property) {
29 7
            $annotation = null;
30 7
            $behavior = null;
31 7
            foreach (self::$collectionAnnotationClasses as $annotationBehavior => $annotationClass) {
32 7
                $annotation = $annotationReader->getPropertyAnnotation($property, $annotationClass);
33 7
                if ($annotation !== null) {
34 6
                    $behavior = $annotationBehavior;
35 6
                    break;
36
                }
37 7
            }
38
39 7
            if ($annotation !== null) {
40
                // get the item name, or deduce it (singularize the property name)
41 6
                $itemName = $annotation->getItemName();
42 6
                if ($itemName === null) {
43 6
                    $itemName = Inflector::singularize($propertyName);
44 6
                }
45
46 6
                $objectCollectionsItemNames["byItemName"][$itemName] = array(
47 6
                    "property" => $propertyName,
48 6
                    "behavior" => $behavior,
49 6
                    "methods" => $annotation->getMethods()
50 6
                );
51 6
                $objectCollectionsItemNames["byProperty"][$propertyName] = array(
52 6
                    "itemName" => $itemName,
53 6
                    "behavior" => $behavior,
54 6
                    "methods" => $annotation->getMethods()
55 6
                );
56 6
            }
57 7
        }
58
59 7
        return $objectCollectionsItemNames;
60
    }
61
}
62