Completed
Pull Request — master (#10)
by Tomáš
04:50 queued 01:55
created

ProxySourceTaxonomyIndexGenerator::generate()   C

Complexity

Conditions 11
Paths 97

Size

Total Lines 59
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
dl 0
loc 59
ccs 0
cts 43
cp 0
rs 6.3545
c 0
b 0
f 0
cc 11
eloc 33
nc 97
nop 1
crap 132

How to fix   Long Method    Complexity   

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
3
/*
4
 * This file is a part of Sculpin.
5
 *
6
 * (c) Dragonfly Development Inc.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Symplify\PHP7_Sculpin\Taxonomy;
13
14
use Symplify\PHP7_Sculpin\DataProvider\DataProviderManager;
15
use Symplify\PHP7_Sculpin\Generator\GeneratorInterface;
16
use Symplify\PHP7_Sculpin\Source\SourceInterface;
17
18
final class ProxySourceTaxonomyIndexGenerator implements GeneratorInterface
19
{
20
    private $dataProviderManager;
21
    private $dataProviderName;
22
    private $injectedTaxonKey;
23
    private $injectedTaxonItemsKey;
24
    private $permalinkStrategyCollection;
25
26
    public function __construct(
27
        DataProviderManager $dataProviderManager,
28
        $dataProviderName,
29
        $injectedTaxonKey,
30
        $injectedTaxonItemsKey,
31
        PermalinkStrategyCollection $permalinkStrategyCollection
32
    ) {
33
        $this->dataProviderManager = $dataProviderManager;
34
        $this->dataProviderName = $dataProviderName; // post_tags
35
        $this->injectedTaxonKey = $injectedTaxonKey; // tag
36
        $this->injectedTaxonItemsKey = $injectedTaxonItemsKey; // tagged_posts
37
        $this->permalinkStrategyCollection = $permalinkStrategyCollection;
38
    }
39
40
    public function generate(SourceInterface $source) : array
41
    {
42
        $dataProvider = $this->dataProviderManager->dataProvider($this->dataProviderName);
43
        $taxons = $dataProvider->provideData();
44
45
        $generatedSources = [];
46
        foreach ($taxons as $taxon => $items) {
47
            $generatedSource = $source->duplicate(
48
                $source->sourceId().':'.$this->injectedTaxonKey.'='.$taxon
49
            );
50
51
            $permalink = $source->data()->get('permalink') ?: $source->relativePathname();
52
            $basename = basename($permalink);
53
54
            $permalink = dirname($permalink);
55
56
            $indexType = null;
57
58
            if (preg_match('/^(.+?)\.(.+)$/', $basename, $matches)) {
59
                $urlTaxon = $this->permalinkStrategyCollection->process($taxon);
60
                $indexType = $matches[2];
61
                $suffix = in_array($indexType, ['xml', 'rss', 'json']) ? '.'.$indexType : '/';
62
                $permalink = $permalink.'/'.$urlTaxon.$suffix;
63
            } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
64
                // not sure what case this is?
65
            }
66
67
            if (0 === strpos($permalink, './')) {
68
                $permalink = substr($permalink, 2);
69
            }
70
71
            if (0 !== strpos($permalink, '/')) {
72
                $permalink = '/'.$permalink;
73
            }
74
75
            if ($permalink) {
76
                // not sure if this is ever going to happen?
77
                $generatedSource->data()->set('permalink', $permalink);
78
            }
79
80
            $generatedSource->data()->set($this->injectedTaxonKey, $taxon);
81
            $generatedSource->data()->set($this->injectedTaxonItemsKey, $items);
82
83
            if ($indexType) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $indexType of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
84
                foreach ($items as $item) {
85
                    $key = $this->injectedTaxonKey.'_'.$indexType.'_index_permalinks';
86
                    $taxonIndexPermalinks = $item->data()->get($key) ?: [];
87
88
                    $taxonIndexPermalinks[$taxon] = $permalink;
89
90
                    $item->data()->set($key, $taxonIndexPermalinks);
91
                }
92
            }
93
94
            $generatedSources[] = $generatedSource;
95
        }
96
97
        return $generatedSources;
98
    }
99
}
100