Completed
Pull Request — master (#107)
by Glenn
104:21 queued 64:56
created

DepartmentOutput::includeManagers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace App\Http\TransformersApi;
4
5
use App\Departments;
6
use App\Http\TransformersApi\Relations\DepartmentManager;
7
use League\Fractal;
8
9
/**
10
 * Class DepartmentTransformer
11
 */
12
class DepartmentOutput extends Fractal\TransformerAbstract
13
{
14
    /**
15
     * List of resources to automatically include
16
     *
17
     * @var array
18
     */
19
    protected $defaultIncludes = ['managers'];
20
    
21
    /**
22
     * Generic array
23
     *
24
     * @param  Departments $department
25
     * @return array
26
     */
27
    public function transform(Departments $department)
28
    {
29
        return [
30
            'id'           => (int)    $department->id,
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<App\Departments>. 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...
31
            'name'         => (string) $department->department_name,
32
            'managers'     => (string) $department->department_manager,
33
            'description'  => (string) $department->department_description
34
        ];
35
    }
36
37
    /**
38
     * Include Managers
39
     *
40
     * @param  Departments $department
41
     * @return Fractal\Resource\Item
42
     */
43
    public function includeManagers(Departments $department)
44
    {
45
        $data = $department->managers;
0 ignored issues
show
Documentation introduced by
The property managers does not exist on object<App\Departments>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
46
        return $this->collection($data, new DepartmentManager);
0 ignored issues
show
Documentation introduced by
new \App\Http\Transforme...ons\DepartmentManager() is of type object<App\Http\Transfor...ions\DepartmentManager>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
47
    }
48
}