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.

ArtifactAttachmentXMLLinker::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
/**
3
 * Copyright (c) Enalean, 2014. 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
class ArtifactAttachmentXMLLinker implements ArtifactAttachmentXMLExporter {
22
23
    /** @var ArtifactXMLNodeHelper */
24
    private $node_helper;
25
26
    /** @var ArtifactXMLExporterDao */
27
    private $dao;
28
29
    public function __construct(ArtifactXMLNodeHelper $node_helper, ArtifactXMLExporterDao $dao) {
30
        $this->node_helper = $node_helper;
31
        $this->dao         = $dao;
32
    }
33
34
    public function addFilesToArtifact(DOMElement $artifact_node, $artifact_type_id, $artifact_id) {
35
        $dar = $this->dao->searchFilesForArtifact($artifact_id);
36
        foreach($dar as $row) {
0 ignored issues
show
Bug introduced by
The expression $dar 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...
37
            $xml_file_id     = ArtifactAttachmentFieldXMLExporter::XML_FILE_PREFIX.$row['id'];
38
39
            $file = $this->node_helper->createElement('file');
40
            $file->setAttribute('id', $xml_file_id);
41
            $file->appendChild($this->node_helper->getNodeWithValue('filename', $row['filename']));
42
            $file->appendChild($this->node_helper->getNodeWithValue('path', $this->getPathRelativeToTv3RootPath($artifact_type_id, $row['id'])));
43
            $file->appendChild($this->node_helper->getNodeWithValue('filesize', $row['filesize']));
44
            $file->appendChild($this->node_helper->getNodeWithValue('filetype', $row['filetype']));
45
            $file->appendChild($this->node_helper->getNodeWithValue('description', $row['description']));
46
            $artifact_node->appendChild($file);
47
        }
48
    }
49
50
    private function getPathRelativeToTv3RootPath($artifact_type_id, $attachment_id) {
51
        $full_path = $this->getFilePathOnServer($artifact_type_id, $attachment_id);
52
53
        return dirname($full_path) . DIRECTORY_SEPARATOR . basename($full_path);
54
    }
55
56
    private function getFilePathOnServer($artifact_type_id, $attachment_id) {
57
        return ArtifactFile::getPathOnFilesystemByArtifactTypeId($artifact_type_id, $attachment_id);
58
    }
59
}
60