Completed
Push — master ( 4f4aae...6745e3 )
by Vitaly
02:58
created

Gallery   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 1 Features 2
Metric Value
wmc 5
c 4
b 1
f 2
lcom 1
cbo 2
dl 0
loc 46
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B analyze() 0 37 5
1
<?php
2
//[PHPCOMPRESSOR(remove,start)]
3
/**
4
 * Created by Vitaly Iegorov <[email protected]>.
5
 * on 23.03.16 at 11:45
6
 */
7
namespace samsoncms\api\generator\analyzer;
8
9
use samsoncms\api\Field;
10
use samsoncms\api\generator\exception\ParentEntityNotFound;
11
use samsoncms\api\Navigation;
12
13
/**
14
 * Generic entities metadata analyzer.
15
 *
16
 * @package samsoncms\api\analyzer
17
 */
18
class Gallery extends Virtual
19
{
20
    /**
21
     * Analyze virtual entities and gather their metadata.
22
     *
23
     * @return \samsoncms\api\generator\metadata\Virtual[]
24
     * @throws ParentEntityNotFound
25
     */
26
    public function analyze()
27
    {
28
        $metadataCollection = [];
29
30
        // Iterate all structures, parents first
31
        foreach ($this->getVirtualEntities() as $structureRow) {
32
            // Fill in entity metadata
33
            $metadata = new \samsoncms\api\generator\metadata\Gallery();
34
            $navigationID = $structureRow[Navigation::F_PRIMARY];
35
36
            // Iterate entity fields
37
            foreach ($this->getEntityFields($navigationID) as $fieldID => $fieldRow) {
38
                // We need only gallery fields
39
                if ((int)$fieldRow[Field::F_TYPE] === Field::TYPE_GALLERY) {
40
                    $metadata->parentClassName = $this->entityName($structureRow[Navigation::F_NAME]);
0 ignored issues
show
Documentation Bug introduced by
The property $parentClassName was declared of type integer, but $this->entityName($struc...pi\Navigation::F_NAME]) is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
41
                    // Avoid GalleryGallery
42
                    $metadata->entity = ucfirst($this->fieldName($fieldRow[Field::F_IDENTIFIER]));
43
                    // Avoid GalleryGallery
44
                    $metadata->entity = $metadata->entity !== 'Gallery' ? $metadata->entity . 'Gallery' : $metadata->entity;
45
                    // Prepend Entity name
46
                    $metadata->entity = $metadata->parentClassName.$metadata->entity;
47
                    $metadata->entityClassName = $this->fullEntityName($metadata->entity);
48
                    $metadata->realName = $fieldRow[Field::F_IDENTIFIER];
49
                    $metadata->fieldID = $fieldID;
0 ignored issues
show
Documentation Bug introduced by
It seems like $fieldID can also be of type string. However, the property $fieldID is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
50
                    $metadata->parentID = $navigationID;
51
52
                    // Store metadata by entity identifier
53
                    $metadataCollection[$navigationID] = $metadata;
54
55
                    // Store global collection
56
                    self::$metadata[$navigationID] = $metadata;
57
                }
58
            }
59
        }
60
61
        return $metadataCollection;
62
    }
63
}
64
//[PHPCOMPRESSOR(remove,end)]