UserDistributionAnalytic::build()   B
last analyzed

Complexity

Conditions 6
Paths 15

Size

Total Lines 34
Code Lines 20

Duplication

Lines 3
Ratio 8.82 %

Importance

Changes 0
Metric Value
cc 6
eloc 20
nc 15
nop 1
dl 3
loc 34
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of Jitamin.
5
 *
6
 * Copyright (C) Jitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Jitamin\Analytic;
13
14
use Jitamin\Foundation\Base;
15
16
/**
17
 * User Distribution.
18
 */
19
class UserDistributionAnalytic extends Base
20
{
21
    /**
22
     * Build Report.
23
     *
24
     * @param int $project_id
25
     *
26
     * @return array
27
     */
28
    public function build($project_id)
29
    {
30
        $metrics = [];
31
        $total = 0;
32
        $tasks = $this->taskFinderModel->getAll($project_id);
0 ignored issues
show
Documentation introduced by
The property taskFinderModel does not exist on object<Jitamin\Analytic\UserDistributionAnalytic>. 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...
33
        $users = $this->projectUserRoleModel->getAssignableUsersList($project_id);
0 ignored issues
show
Documentation introduced by
The property projectUserRoleModel does not exist on object<Jitamin\Analytic\UserDistributionAnalytic>. 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...
34
35
        foreach ($tasks as $task) {
36
            $user = isset($users[$task['owner_id']]) ? $users[$task['owner_id']] : $users[0];
37
            $total++;
38
39
            if (!isset($metrics[$user])) {
40
                $metrics[$user] = [
41
                    'nb_tasks'   => 0,
42
                    'percentage' => 0,
43
                    'user'       => $user,
44
                ];
45
            }
46
47
            $metrics[$user]['nb_tasks']++;
48
        }
49
50
        if ($total === 0) {
51
            return [];
52
        }
53
54 View Code Duplication
        foreach ($metrics as &$metric) {
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...
55
            $metric['percentage'] = round(($metric['nb_tasks'] * 100) / $total, 2);
56
        }
57
58
        ksort($metrics);
59
60
        return array_values($metrics);
61
    }
62
}
63