Completed
Push — master ( 674961...f4f264 )
by Antarès
03:25
created

AutoConstructTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 64
Duplicated Lines 21.88 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 91.43%

Importance

Changes 13
Bugs 0 Features 3
Metric Value
wmc 11
c 13
b 0
f 3
lcom 1
cbo 2
dl 14
loc 64
ccs 32
cts 35
cp 0.9143
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C initializeProperties() 14 48 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
        foreach ($this->_initialPropertiesValues as $propertyName => $value) {
1 ignored issue
show
Bug introduced by
The property _initialPropertiesValues 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...
31 29
            if ($initializeValueValidationEnabled) {
32 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...
33 29
            }
34
35 29
            $this->$propertyName = $value;
36
37 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...
38 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...
39 29
            } else {
40 29
                foreach ($value as $newValue) {
41 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...
42 29
                }
43
            }
44 29
        }
45
46
        // Initialize the propeties using given arguments
47 29
        if ($this->_initializationNeededArguments !== null && $properties !== null) {
48 3
            $numberOfNeededArguments = count($this->_initializationNeededArguments);
1 ignored issue
show
Bug introduced by
The property _initializationNeededArguments 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...
49
50 3
            MethodCallManager::assertArgsNumber($numberOfNeededArguments, $properties);
51
52 3
            for ($i = 0; $i < $numberOfNeededArguments; $i++) {
53 3
                $property = $this->_initializationNeededArguments[$i];
54 3
                $argument = $properties[$i];
55
56 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...
57
58 2
                $this->$property = $argument;
59
60
                // Manage associations
61 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...
62 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...
63 2
                } else {
64
                    foreach ($argument as $value) {
65
                        $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...
66
                    }
67
                }
68 2
            }
69 2
        }
70 28
    }
71
}
72