Passed
Push — feature/initial-implementation ( f3915b...fbd338 )
by Fike
02:22
created

Compiler::convert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AmaTeam\ElasticSearch\Mapping\Conversion;
6
7
use AmaTeam\ElasticSearch\API\Entity\Mapping\ClassMappingInterface;
8
use AmaTeam\ElasticSearch\API\Entity\Mapping\PropertyMappingInterface;
9
use AmaTeam\ElasticSearch\API\Entity\Mapping\ProviderInterface;
10
use AmaTeam\ElasticSearch\API\Mapping\MappingInterface;
11
use AmaTeam\ElasticSearch\API\Mapping\Conversion\CompilerInterface;
12
use AmaTeam\ElasticSearch\API\Mapping\Conversion\ContextInterface;
13
use AmaTeam\ElasticSearch\API\Mapping\Conversion\DefaultContext;
14
use AmaTeam\ElasticSearch\API\Entity\Mapping\ClassMappingView;
15
use AmaTeam\ElasticSearch\Entity\Provider;
16
use AmaTeam\ElasticSearch\Mapping\Mapping;
17
use AmaTeam\ElasticSearch\API\Entity\Mapping\PropertyMappingView;
18
use AmaTeam\ElasticSearch\Mapping\Type\RootType;
19
20
class Compiler implements CompilerInterface
21
{
22
    /**
23
     * @var ProviderInterface
24
     */
25
    private $provider;
26
27
    /**
28
     * @param ProviderInterface $provider
29
     */
30
    public function __construct(ProviderInterface $provider = null)
31
    {
32
        $this->provider = $provider ?? new Provider();
0 ignored issues
show
Documentation Bug introduced by
It seems like $provider ?? new AmaTeam...earch\Entity\Provider() can also be of type AmaTeam\ElasticSearch\Entity\Provider. However, the property $provider is declared as type AmaTeam\ElasticSearch\AP...pping\ProviderInterface. 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...
33
    }
34
35
    public function convert(ClassMappingInterface $source, ContextInterface $context = null): MappingInterface
36
    {
37
        $context = $context ?? new DefaultContext();
38
        return $this->renderDocumentMapping($source, $context);
39
    }
40
41
    private function renderDocumentMapping(ClassMappingInterface $document, ContextInterface $context): MappingInterface
42
    {
43
        $views = array_map([$document, 'getView'], $context->getViews());
44
        array_unshift($views, $document->getDefaultView());
45
        $view = ClassMappingView::merge(...array_filter($views));
46
        $mapping = new Mapping();
47
        $type = $context->isRootMapping() ? RootType::ID : $view->getType();
48
        if ($type) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $type of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
49
            $mapping->setType($type);
50
        }
51
        $properties = [];
52
        foreach ($document->getProperties() as $property => $definition) {
53
            if (in_array($property, $view->getIgnoredProperties())) {
54
                continue;
55
            }
56
            $innerContext = DefaultContext::from($context)->setRootMapping(false);
57
            $rendered = $this->compilePropertyMapping($definition, $innerContext);
58
            if ($rendered->getType()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $rendered->getType() of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
59
                $properties[$property] = $rendered;
60
            }
61
        }
62
        $mapping->setProperties($properties);
63
        $mapping->setParameters($view->getParameters());
64
        return $mapping;
65
    }
66
67
    private function compilePropertyMapping(
68
        PropertyMappingInterface $property,
69
        ContextInterface $context
70
    ): MappingInterface {
71
        $views = array_map([$property, 'getView'], $context->getViews());
72
        array_unshift($views, $property->getDefaultView());
73
        $view = PropertyMappingView::merge(...array_filter($views));
74
        $mapping = new Mapping();
75
        if ($view->getTargetClass()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $view->getTargetClass() of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
76
            $localViewNames = $context->getViews();
77
            if (!empty($property->getForcedViewNames())) {
78
                $forcedViews = $property->getForcedViewNames();
79
                $append = $property->shouldAppendForcedViews();
80
                $localViewNames = $append ? array_merge($localViewNames, $forcedViews) : $forcedViews;
81
            }
82
            $source = $this->provider->get($view->getTargetClass());
83
            $innerContext = DefaultContext::from($context)->setViews($localViewNames);
84
            $mapping = $this->renderDocumentMapping($source, $innerContext);
85
        }
86
        foreach ($view->getParameters() as $parameter => $value) {
87
            $mapping->setParameter($parameter, $value);
88
        }
89
        if ($view->getType()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $view->getType() of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
90
            $mapping->setType($view->getType());
91
        }
92
        return $mapping;
93
    }
94
}
95