Completed
Push — master ( 9b25f2...d42705 )
by Tomáš
09:42 queued 07:08
created

RenderableFilesProcessor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 17
nc 1
nop 8
crap 2

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Symplify
7
 * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
8
 */
9
10
namespace Symplify\PHP7_Sculpin\Renderable;
11
12
use SplFileInfo;
13
use Symplify\PHP7_Sculpin\Output\FileSystemWriter;
14
use Symplify\PHP7_Sculpin\Configuration\Configuration;
15
use Symplify\PHP7_Sculpin\Renderable\Configuration\ConfigurationDecorator;
16
use Symplify\PHP7_Sculpin\Renderable\File\FileFactory;
17
use Symplify\PHP7_Sculpin\Renderable\File\PostFile;
18
use Symplify\PHP7_Sculpin\Renderable\Latte\LatteDecorator;
19
use Symplify\PHP7_Sculpin\Renderable\Markdown\MarkdownDecorator;
20
use Symplify\PHP7_Sculpin\Renderable\Routing\RouteDecorator;
21
use Symplify\PHP7_Sculpin\Renderable\Sorting\PostSorter;
22
23
final class RenderableFilesProcessor
24
{
25
    /**
26
     * @var FileFactory
27
     */
28
    private $fileFactory;
29
30
    /**
31
     * @var RouteDecorator
32
     */
33
    private $routeDecorator;
34
35
    /**
36
     * @var PostSorter
37
     */
38
    private $postSorter;
39
40
    /**
41
     * @var ConfigurationDecorator
42
     */
43
    private $configurationDecorator;
44
45
    /**
46
     * @var MarkdownDecorator
47
     */
48
    private $markdownDecorator;
49
    /**
50
     * @var LatteDecorator
51
     */
52
    private $latteDecorator;
53
54
    /**
55
     * @var FileSystemWriter
56
     */
57
    private $fileSystemWriter;
58
    /**
59
     * @var Configuration
60
     */
61
    private $configuration;
62
63
    public function __construct(
64
        FileFactory $fileFactory,
65
        RouteDecorator $routeDecorator,
66
        PostSorter $postSorter,
67
        ConfigurationDecorator $configurationDecorator,
68
        MarkdownDecorator $markdownDecorator,
69
        LatteDecorator $latteDecorator,
70
        FileSystemWriter $fileSystemWriter,
71
        Configuration $configuration
72
    ) {
73
        $this->fileFactory = $fileFactory;
74
        $this->routeDecorator = $routeDecorator;
75
        $this->postSorter = $postSorter;
76
        $this->configurationDecorator = $configurationDecorator;
77
        $this->markdownDecorator = $markdownDecorator;
78
        $this->latteDecorator = $latteDecorator;
79
        $this->fileSystemWriter = $fileSystemWriter;
80
        $this->configuration = $configuration;
81
    }
82
83
    /**
84
     * @param SplFileInfo[] $files
85
     */
86
    public function processFiles(array $files)
87
    {
88
        if (!count($files)) {
89
            return;
90
        }
91
92
        // 1. create file objects
93
        foreach ($files as $id => $file) {
94
            $files[$id] = $this->fileFactory->create($file);
0 ignored issues
show
Bug introduced by
It seems like $file defined by $file on line 93 can also be of type object<Symplify\PHP7_Scu...n\Renderable\File\File>; however, Symplify\PHP7_Sculpin\Re...e\FileFactory::create() does only seem to accept object<SplFileInfo>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
95
        }
96
97
        // 2. set routes
98
        foreach ($files as $file) {
99
            $this->routeDecorator->decorateFile($file);
0 ignored issues
show
Bug introduced by
It seems like $file defined by $file on line 98 can also be of type object<SplFileInfo>; however, Symplify\PHP7_Sculpin\Re...corator::decorateFile() does only seem to accept object<Symplify\PHP7_Scu...n\Renderable\File\File>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
100
        }
101
102
        // 3. order (should be performed before render)
103
        if ($files[0] instanceof PostFile) {
104
            $files = $this->postSorter->sort($files);
0 ignored issues
show
Documentation introduced by
$files is of type array<integer,object<Spl...ble\\File\\PostFile>"}>, but the function expects a array<integer,object<Sym...derable\File\PostFile>>.

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...
105
106
            // set posts to configuration
107
            $this->configuration->addOption('posts', $files);
108
        }
109
110
        // 4. detect and set options
111
        foreach ($files as $file) {
112
            $this->configurationDecorator->decorateFile($file);
0 ignored issues
show
Bug introduced by
It seems like $file defined by $file on line 111 can also be of type object<SplFileInfo>; however, Symplify\PHP7_Sculpin\Re...corator::decorateFile() does only seem to accept object<Symplify\PHP7_Scu...n\Renderable\File\File>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
113
        }
114
115
        // 5. format Markdown to HTML
116
        foreach ($files as $file) {
117
            $this->markdownDecorator->decorateFile($file);
0 ignored issues
show
Bug introduced by
It seems like $file defined by $file on line 116 can also be of type object<SplFileInfo>; however, Symplify\PHP7_Sculpin\Re...corator::decorateFile() does only seem to accept object<Symplify\PHP7_Scu...n\Renderable\File\File>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
118
        }
119
120
        // 6. format Latte to HTML
121
        foreach ($files as $file) {
122
            $this->latteDecorator->decorateFile($file);
0 ignored issues
show
Bug introduced by
It seems like $file defined by $file on line 121 can also be of type object<SplFileInfo>; however, Symplify\PHP7_Sculpin\Re...corator::decorateFile() does only seem to accept object<Symplify\PHP7_Scu...n\Renderable\File\File>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
123
        }
124
125
        // 7. save files
126
        $this->fileSystemWriter->copyRenderableFiles($files);
0 ignored issues
show
Documentation introduced by
$files is of type array<integer,object<Spl...\Renderable\File\File>>, but the function expects a array<integer,object<Sym...\Renderable\File\File>>.

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...
127
    }
128
}
129