Completed
Push — master ( 0645c0...b651ea )
by Jaap
03:02
created

InitializeBuilderFromConfig::buildVersion()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 25
ccs 0
cts 15
cp 0
crap 12
rs 9.52
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of phpDocumentor.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @link https://phpdoc.org
12
 */
13
14
namespace phpDocumentor\Pipeline\Stage;
15
16
use phpDocumentor\Configuration\VersionSpecification;
17
use phpDocumentor\Descriptor\ApiSetDescriptor;
18
use phpDocumentor\Descriptor\Collection;
19
use phpDocumentor\Descriptor\Collection as PartialsCollection;
20
use phpDocumentor\Descriptor\DocumentationSetDescriptor;
21
use phpDocumentor\Descriptor\GuideSetDescriptor;
22
use phpDocumentor\Descriptor\VersionDescriptor;
23
use function md5;
24
25
final class InitializeBuilderFromConfig
26
{
27
    /** @var PartialsCollection<string> */
28
    private $partials;
29
30
    /**
31
     * @param PartialsCollection<string> $partials
0 ignored issues
show
Documentation introduced by
The doc-type PartialsCollection<string> could not be parsed: Expected "|" or "end of type", but got "<" at position 18. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
32
     */
33 1
    public function __construct(PartialsCollection $partials)
34
    {
35 1
        $this->partials = $partials;
36 1
    }
37
38 1
    public function __invoke(Payload $payload) : Payload
39
    {
40 1
        $configuration = $payload->getConfig();
41
42 1
        $builder = $payload->getBuilder();
43 1
        $builder->createProjectDescriptor();
44 1
        $builder->setName($configuration['phpdocumentor']['title'] ?? '');
45 1
        $builder->setPartials($this->partials);
46 1
        $builder->setCustomSettings($configuration['phpdocumentor']['settings'] ?? []);
47
48 1
        foreach ($configuration['phpdocumentor']['versions'] as $version) {
49
            $builder->addVersion(
50
                $this->buildVersion(
51
                    $version
52
                )
53
            );
54
        }
55
56 1
        return $payload;
57
    }
58
59
    private function buildVersion(VersionSpecification $version) : VersionDescriptor
60
    {
61
        $collection = Collection::fromClassString(DocumentationSetDescriptor::class);
62
        foreach ($version->getGuides() as $guide) {
0 ignored issues
show
Bug introduced by
The expression $version->getGuides() of type array<integer,*>|null 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...
63
            $collection->add(
64
                new GuideSetDescriptor(md5($guide['output']), $guide['source'], $guide['output'], $guide['format'])
0 ignored issues
show
Documentation introduced by
new \phpDocumentor\Descr...ut'], $guide['format']) is of type object<phpDocumentor\Des...tor\GuideSetDescriptor>, but the function expects a object<phpDocumentor\Descriptor\T>.

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...
65
            );
66
        }
67
68
        foreach ($version->getApi() as $apiSpecification) {
69
            $collection->add(
70
                new ApiSetDescriptor(
0 ignored issues
show
Documentation introduced by
new \phpDocumentor\Descr...t'], $apiSpecification) is of type object<phpDocumentor\Descriptor\ApiSetDescriptor>, but the function expects a object<phpDocumentor\Descriptor\T>.

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...
71
                    md5($apiSpecification['output']),
72
                    $apiSpecification['source'],
73
                    $apiSpecification['output'],
74
                    $apiSpecification
75
                )
76
            );
77
        }
78
79
        return new VersionDescriptor(
80
            $version->getNumber(),
81
            $collection
82
        );
83
    }
84
}
85