HierarchyTransformerTrait::setupHierarchy()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php namespace App\LaravelRestCms;
2
3
trait HierarchyTransformerTrait {
4
5
	/**
6
	 * The transformer of the parent (usually itself)
7
	 * 
8
	 * @var string
9
	 */
10
	protected $parentTransformer;
11
12
	/**
13
	 * Sets instance vars and adds includes
14
	 * 
15
	 * @param  string $parentTransformer 
16
	 * @param  string $method            
17
	 */
18
	protected function setupHierarchy($parentTransformer, $method = 'parent')
19
	{
20
		$this->parentTransformer = $parentTransformer;
21
		parent::addToIncludes($method);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (addToIncludes() instead of setupHierarchy()). Are you sure this is correct? If so, you might want to change this to $this->addToIncludes().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
22
	}
23
24
	/**
25
	 * Include Parent
26
	 * 
27
	 * @param \App\LaravelRestCms\BaseModel
28
	 * @return \League\Fractal\ItemResource
29
	 */
30
	public function includeParent(BaseModel $model)
31
	{
32
		return $this->collection($model->parent, new $this->parentTransformer);
0 ignored issues
show
Documentation introduced by
The property parent does not exist on object<App\LaravelRestCms\BaseModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Bug introduced by
It seems like collection() 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
	}
34
    
35
}