GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

buildSwimlineForArtifact()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 11
rs 9.4285
cc 2
eloc 8
nc 2
nop 3
1
<?php
2
/**
3
 * Copyright (c) Enalean, 2013. All Rights Reserved.
4
 *
5
 * This file is a part of Tuleap.
6
 *
7
 * Tuleap is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 2 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * Tuleap is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with Tuleap. If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
/**
22
 * Build the artifact tree to be presented on the cardwall
23
 */
24
class Cardwall_PaneBoardBuilder {
25
26
    private $artifact_factory;
27
    private $presenter_builder;
28
    private $dao;
29
    private $swimline_factory;
30
31
    public function __construct(Cardwall_CardInCellPresenterBuilder $presenter_builder, Tracker_ArtifactFactory $artifact_factory, AgileDashboard_BacklogItemDao $dao, Cardwall_SwimlineFactory $swimline_factory) {
32
        $this->presenter_builder = $presenter_builder;
33
        $this->artifact_factory = $artifact_factory;
34
        $this->dao = $dao;
35
        $this->swimline_factory = $swimline_factory;
36
    }
37
38
    /**
39
     * Get the board
40
     *
41
     * @param PFUser $user
42
     * @param Tracker_Artifact $milestone_artifact
43
     * @param Cardwall_OnTop_Config_ColumnCollection $columns
44
     * @param Cardwall_MappingCollection $mapping_collection
45
     * @return \Cardwall_Board
46
     */
47
    public function getBoard(PFUser $user, Tracker_Artifact $milestone_artifact, Cardwall_OnTop_Config_ColumnCollection $columns, Cardwall_MappingCollection $mapping_collection) {
48
        return new Cardwall_Board(
49
            $this->getSwimlines($user, $milestone_artifact, $columns),
50
            $columns,
51
            $mapping_collection
52
        );
53
    }
54
55
    /**
56
     * Retrieves the artifacts planned for the given milestone artifact.
57
     *
58
     * @param PFUser $user
59
     * @param Tracker_Artifact $milestone_artifact
60
     * @param Cardwall_OnTop_Config_ColumnCollection $columns
61
     *
62
     * @return Cardwall_Swimline[]
63
     */
64
    private function getSwimlines(PFUser $user, Tracker_Artifact $milestone_artifact, Cardwall_OnTop_Config_ColumnCollection $columns) {
65
        $swimlines = array();
66
        foreach ($this->dao->getBacklogArtifacts($milestone_artifact->getId()) as $row) {
0 ignored issues
show
Bug introduced by
The expression $this->dao->getBacklogAr...tone_artifact->getId()) of type false|object is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
67
            $swimline_artifact = $this->artifact_factory->getInstanceFromRow($row);
68
            if ($swimline_artifact->userCanView($user)) {
69
                $swimlines[] = $this->buildSwimlineForArtifact($user, $swimline_artifact, $columns);
70
            }
71
        }
72
        return $swimlines;
73
    }
74
75
    private function buildSwimlineForArtifact(PFUser $user, Tracker_Artifact $artifact, Cardwall_OnTop_Config_ColumnCollection $columns) {
76
        $artifact_presenter = $this->presenter_builder->getCardInCellPresenter($artifact, $artifact->getId());
77
        $children           = $artifact->getChildrenForUser($user);
78
79
        if ($children) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $children of type Tracker_Artifact[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
80
            $children_presenters = $this->presenter_builder->getCardInCellPresenters($children, $artifact->getId());
81
            return $this->buildSwimline($artifact_presenter, $columns, $children_presenters);
82
        } else {
83
            return $this->buildSwimlineSolo($artifact, $artifact_presenter, $columns);
84
        }
85
    }
86
87
    private function buildSwimlineSolo(Tracker_Artifact $artifact, Cardwall_CardInCellPresenter $artifact_presenter, Cardwall_OnTop_Config_ColumnCollection $columns) {
88
        $cells = $this->swimline_factory->getCells($columns, array($artifact_presenter));
89
90
        if ($this->areSwimlineCellsEmpty($cells)) {
91
            return $this->buildSwimlineSoloNoMatchingColumns($artifact_presenter, $artifact, $cells);
92
        }
93
94
        return new Cardwall_SwimlineSolo(
95
            $artifact->getId(),
96
            $cells
97
        );
98
    }
99
100
    private function areSwimlineCellsEmpty(array $cells) {
101
        foreach ($cells as $cell) {
102
            if ($cell['cardincell_presenters']) {
103
                return false;
104
            }
105
        }
106
107
        return true;
108
    }
109
110
    private function buildSwimline(Cardwall_CardInCellPresenter $artifact_presenter, Cardwall_OnTop_Config_ColumnCollection $columns, array $children_presenters) {
111
        return new Cardwall_Swimline(
112
            $artifact_presenter,
113
            $this->swimline_factory->getCells($columns, $children_presenters)
114
        );
115
    }
116
117
    private function buildSwimlineSoloNoMatchingColumns(Cardwall_CardInCellPresenter $artifact_presenter, Tracker_Artifact $artifact, array $cells) {
118
        return new Cardwall_SwimlineSoloNoMatchingColumns(
119
            $artifact_presenter,
120
            $artifact,
121
            $cells
122
        );
123
    }
124
}
125
126
?>
127