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

AutoConstructTrait::initializeProperties()   C

Complexity

Conditions 10
Paths 20

Size

Total Lines 51
Code Lines 27

Duplication

Lines 14
Ratio 27.45 %

Code Coverage

Tests 31
CRAP Score 10.0686

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 14
loc 51
ccs 31
cts 34
cp 0.9118
rs 6
cc 10
eloc 27
nc 20
nop 1
crap 10.0686

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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