GalleryAnalyzer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 48
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B analyze() 0 39 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\generator\metadata\VirtualMetadata;
12
use samsoncms\api\Navigation;
13
14
/**
15
 * Generic entities metadata analyzer.
16
 *
17
 * @package samsoncms\api\analyzer
18
 */
19
class GalleryAnalyzer extends VirtualAnalyzer
20
{
21
    /**
22
     * Analyze virtual entities and gather their metadata.
23
     *
24
     * @return \samsoncms\api\generator\metadata\VirtualMetadata[]
25
     * @throws ParentEntityNotFound
26
     */
27
    public function analyze()
28
    {
29
        /** @var VirtualMetadata[] $metadataCollection Set pointer to global metadata collection */
30
        $metadataCollection = [];
31
32
        // Iterate all structures, parents first
33
        foreach ($this->getVirtualEntities() as $structureRow) {
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) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
40
41
                    // Avoid GalleryGallery
42
                    $entity = ucfirst($this->fieldName($fieldRow[Field::F_IDENTIFIER]));
43
                    // Avoid GalleryGallery
44
                    $entity = $entity !== 'Gallery' ? $entity . 'Gallery' : $entity;
45
                    $className = $this->fullEntityName($entity);
46
47
                    // Fill in entity metadata
48
                    $metadata = new \samsoncms\api\generator\metadata\GalleryMetadata($className);
49
50
                    $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...
51
                    $metadata->entity = $entity;
52
                    // Prepend Entity name
53
                    $metadata->entity = $metadata->parentClassName . $metadata->entity;
54
                    $metadata->realName = $fieldRow[Field::F_IDENTIFIER];
55
                    $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...
56
                    $metadata->parentID = $navigationID;
57
58
                    // Store metadata by entity identifier
59
                    $metadataCollection[$navigationID] = $metadata;
60
                }
61
            }
62
        }
63
64
        return $metadataCollection;
0 ignored issues
show
Best Practice introduced by
The expression return $metadataCollection; seems to be an array, but some of its elements' types (samsoncms\api\generator\metadata\GalleryMetadata) are incompatible with the return type of the parent method samsoncms\api\generator\...irtualAnalyzer::analyze of type array<samsoncms\api\gene...tadata\VirtualMetadata>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
65
    }
66
}
67
//[PHPCOMPRESSOR(remove,end)]