Test Failed
Pull Request — master (#9)
by Yo
03:11 queued 01:13
created

TemplateListFactory::create()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 66
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 66
ccs 0
cts 58
cp 0
rs 8.6045
c 0
b 0
f 0
cc 6
eloc 46
nc 24
nop 1
crap 42

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Yoanm\DefaultPhpRepository\Factory;
3
4
use Symfony\Component\Finder\Finder;
5
use Yoanm\DefaultPhpRepository\Command\RepositoryType;
6
use Yoanm\DefaultPhpRepository\Helper\TemplateHelper;
7
use Yoanm\DefaultPhpRepository\Model\FolderTemplate;
8
use Yoanm\DefaultPhpRepository\Model\Template;
9
10
/**
11
 * Class TemplateListFactory
12
 */
13
class TemplateListFactory
14
{
15
    /**
16
     * @param string $repositoryType
17
     *
18
     * @return Template[]
19
     */
20
    public function create($repositoryType)
21
    {
22
        $fileTemplateList = [
23
            'git.readme' => 'README.md.twig',
24
            'git.license' => 'LICENSE.twig',
25
            'git.contributing' => 'CONTRIBUTING.md.twig',
26
            'git.gitignore' => '.gitignore.twig',
27
            'composer.config' => 'composer.json.twig',
28
            'phpcs.config' => 'phpcs.xml.dist.twig',
29
            'phpunit.config' => 'phpunit.xml.dist.twig',
30
            'behat.config' => 'behat.yml.twig',
31
            'ci.scrutinizer' => '.scrutinizer.yml.twig',
32
        ];
33
34
        if (RepositoryType::LIBRARY === $repositoryType) {
35
            $fileTemplateList['ci.travis'] = '.travis.yml.twig';
36
        }
37
38
        $list = [];
39
        foreach ($fileTemplateList as $templateId => $templateName) {
40
            $list[$templateId] = $this->createTemplate($templateId, $templateName);
41
        }
42
43
        $folderTemplateList = [
44
            'phpunit.folders' => 'tests',
45
            'behat.folders' => 'features',
46
        ];
47
48
        $basePath = TemplateHelper::getTemplateBasePath();
49
        foreach ($folderTemplateList as $templateId => $templateFolder) {
50
            $folderTemplate = $list[$templateId] = new FolderTemplate($templateId, $templateFolder);
51
            // Iterate over files
52
            $count = 0;
53
            $path = realpath(sprintf('%s/base/%s', $basePath, $templateFolder));
54
            $toRemove = sprintf('%s/base/', $basePath);
55
            foreach ((new Finder())->files()->in($path) as $file) {
56
                $folderTemplate->addFile(
57
                    $this->createTemplate(
58
                        sprintf('%s.%s', $templateId, $count),
59
                        str_replace($toRemove, '', $file->getPathname())
60
                    )
61
                );
62
                $count++;
63
            }
64
        }
65
66
        // Reorder final list
67
        $orderedList =  [
68
            'git.readme' => $list['git.readme'],
69
            'git.license' => $list['git.license'],
70
            'git.contributing' => $list['git.contributing'],
71
            'git.gitignore' => $list['git.gitignore'],
72
            'composer.config' => $list['composer.config'],
73
            'phpcs.config' => $list['phpcs.config'],
74
            'phpunit.config' => $list['phpunit.config'],
75
            'phpunit.folders' => $list['phpunit.folders'],
76
            'behat.config' => $list['behat.config'],
77
            'behat.folders' => $list['behat.folders'],
78
            'ci.scrutinizer' => $list['ci.scrutinizer'],
79
        ];
80
        if (RepositoryType::LIBRARY === $repositoryType) {
81
            $orderedList['ci.travis'] = $list['ci.travis'];
82
        }
83
84
        return $orderedList;
85
    }
86
87
    /**
88
     * @param string $id
89
     * @param string $templateName
90
     *
91
     * @return Template
92
     */
93
    protected function createTemplate($id, $templateName)
94
    {
95
        return new Template($id, $templateName, $this->getOutputFilePath($templateName));
96
    }
97
98
99
    /**
100
     * @param string $templateName
101
     *
102
     * @return string
103
     */
104
    protected function getOutputFilePath($templateName)
105
    {
106
        return str_replace('.twig', '', $templateName);
107
    }
108
}
109