Completed
Branch master (c78441)
by Antarès
07:43 queued 02:23
created

AutoConstructTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 67
Duplicated Lines 20.9 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 91.89%

Importance

Changes 12
Bugs 0 Features 3
Metric Value
wmc 11
c 12
b 0
f 3
lcom 1
cbo 3
dl 14
loc 67
ccs 34
cts 37
cp 0.9189
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C initializeProperties() 14 51 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Accessible;
4
5
use \Accessible\MethodManager\MethodCallManager;
6
use \Accessible\Reader\AutoConstructReader;
7
8
trait AutoConstructTrait
9
{
10
    /**
11
     * Directly calls the initialization method.
12
     */
13 28
    public function __construct()
14
    {
15 28
        $this->initializeProperties(func_get_args());
16 27
    }
17
18
    /**
19
     * Initializes the object according to its class specification and given arguments.
20
     *
21
     * @param array $properties The values to give to the properties.
22
     */
23 29
    protected function initializeProperties($properties = null)
24
    {
25 29
        $this->getPropertiesInfo();
1 ignored issue
show
Bug introduced by
It seems like getPropertiesInfo() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
26
27
        // Initialize the properties that were defined using the Initialize / InitializeObject annotations
28 29
        $initializeValueValidationEnabled = Configuration::isInitializeValuesValidationEnabled();
29
30 29
        $initialValues = AutoConstructReader::getPropertiesToInitialize($this);
31 29
        foreach ($initialValues as $propertyName => $value) {
32 29
            if ($initializeValueValidationEnabled) {
33 29
                $this->assertPropertyValue($propertyName, $value);
1 ignored issue
show
Bug introduced by
It seems like assertPropertyValue() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
34 29
            }
35
36 29
            $this->$propertyName = $value;
37
38 29 View Code Duplication
            if (empty($this->_collectionsItemNames['byProperty'][$propertyName])) {
1 ignored issue
show
Bug introduced by
The property _collectionsItemNames does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39 29
                $this->updatePropertyAssociation($propertyName, array("oldValue" => null, "newValue" => $value));
1 ignored issue
show
Bug introduced by
It seems like updatePropertyAssociation() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
40 29
            } else {
41 29
                foreach ($value as $newValue) {
42 8
                    $this->updatePropertyAssociation($propertyName, array("oldValue" => null, "newValue" => $newValue));
1 ignored issue
show
Bug introduced by
It seems like updatePropertyAssociation() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
43 29
                }
44
            }
45 29
        }
46
47
        // Initialize the propeties using given arguments
48 29
        $neededArguments = AutoConstructReader::getConstructArguments($this);
49
50 29
        if ($neededArguments !== null && $properties !== null) {
51 3
            $numberOfNeededArguments = count($neededArguments);
52
53 3
            MethodCallManager::assertArgsNumber($numberOfNeededArguments, $properties);
54
55 3
            for ($i = 0; $i < $numberOfNeededArguments; $i++) {
56 3
                $property = $neededArguments[$i];
57 3
                $argument = $properties[$i];
58
59 3
                $this->assertPropertyValue($property, $argument);
1 ignored issue
show
Bug introduced by
It seems like assertPropertyValue() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
60
61 2
                $this->$property = $argument;
62
63
                // Manage associations
64 2 View Code Duplication
                if (empty($this->_collectionsItemNames['byProperty'][$property])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65 2
                    $this->updatePropertyAssociation($property, array("oldValue" => null, "newValue" => $argument));
1 ignored issue
show
Bug introduced by
It seems like updatePropertyAssociation() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
66 2
                } else {
67
                    foreach ($argument as $value) {
68
                        $this->updatePropertyAssociation($property, array("oldValue" => null, "newValue" => $value));
1 ignored issue
show
Bug introduced by
It seems like updatePropertyAssociation() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
69
                    }
70
                }
71 2
            }
72 2
        }
73 28
    }
74
}
75