Completed
Push — master ( a6c8f9...b97be3 )
by Vladimir
03:52 queued 19s
created

PermalinkDocumentTrait::sanitizePermalink()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 14
cts 14
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 10
nc 4
nop 1
crap 3
1
<?php
2
3
/**
4
 * @copyright 2017 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Document;
9
10
use allejo\stakx\Service;
11
use allejo\stakx\Filesystem\FilesystemLoader as fs;
12
13
trait PermalinkDocumentTrait
14
{
15
    /** @var string */
16
    protected $permalink = null;
17
18
    /** @var array */
19
    protected $redirects = null;
20
21
    /** Extensions that need to be stripped from permalinks. */
22
    private static $extensionsToStrip = ['twig'];
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 13
    public function handleSpecialRedirects()
28
    {
29 13
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 20
    final public function getTargetFile()
35
    {
36 20
        $permalink = $this->getPermalink();
37 20
        $missingFile = (substr($permalink, -1) == '/');
38 20
        $permalink = str_replace('/', DIRECTORY_SEPARATOR, $permalink);
39
40
        if ($missingFile)
41 20
        {
42 12
            $permalink = rtrim($permalink, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'index.html';
43 12
        }
44
45 20
        return ltrim($permalink, DIRECTORY_SEPARATOR);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 39
    final public function getPermalink()
52
    {
53 39
        $this->buildPermalink();
0 ignored issues
show
Bug introduced by
It seems like buildPermalink() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
54
55 39
        $this->permalink = $this->sanitizePermalink($this->permalink);
56 39
        $this->permalink = str_replace(DIRECTORY_SEPARATOR, '/', $this->permalink);
57 39
        $this->permalink = '/' . ltrim($this->permalink, '/'); // Permalinks should always use '/' and not be OS specific
58
59 39
        return $this->permalink;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 14
    final public function getRedirects()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
66
    {
67 14
        if ($this->redirects === null)
68 14
        {
69 3
            $this->getPermalink();
70 3
        }
71
72 14
        $this->handleSpecialRedirects();
73
74 14
        return $this->redirects;
75
    }
76
77
    /**
78
     * Get the permalink based off the location of where the file is relative to the website. This permalink is to be
79
     * used as a fallback in the case that a permalink is not explicitly specified in the Front Matter.
80
     *
81
     * @return string
82
     */
83 3
    final protected function getPathPermalink()
84
    {
85
        // Remove the protocol of the path, if there is one and prepend a '/' to the beginning
86 3
        $cleanPath = preg_replace('/[\w|\d]+:\/\//', '', $this->getRelativeFilePath());
0 ignored issues
show
Bug introduced by
It seems like getRelativeFilePath() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
87 3
        $cleanPath = ltrim($cleanPath, DIRECTORY_SEPARATOR);
88
89
        // Handle vfs:// paths by replacing their forward slashes with the OS appropriate directory separator
90 3
        if (DIRECTORY_SEPARATOR !== '/')
91 3
        {
92
            $cleanPath = str_replace('/', DIRECTORY_SEPARATOR, $cleanPath);
93
        }
94
95
        // Check the first folder and see if it's a data folder (starts with an underscore) intended for stakx
96 3
        $folders = explode(DIRECTORY_SEPARATOR, $cleanPath);
97
98 3
        if (substr($folders[0], 0, 1) === '_')
99 3
        {
100 1
            array_shift($folders);
101 1
        }
102
103 3
        $cleanPath = implode(DIRECTORY_SEPARATOR, $folders);
104
105 3
        return $cleanPath;
106
    }
107
108
    /**
109
     * Sanitize a permalink to remove unsupported characters or multiple '/' and replace spaces with hyphens.
110
     *
111
     * @param string $permalink A permalink
112
     *
113
     * @return string $permalink The sanitized permalink
114
     */
115 39
    final private function sanitizePermalink($permalink)
116
    {
117
        // Remove multiple '/' together
118 39
        $permalink = preg_replace('/\/+/', '/', $permalink);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $permalink. This often makes code more readable.
Loading history...
119
120
        // Replace all spaces with hyphens
121 39
        $permalink = str_replace(' ', '-', $permalink);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $permalink. This often makes code more readable.
Loading history...
122
123
        // Remove all disallowed characters
124 39
        $permalink = preg_replace('/[^0-9a-zA-Z-_\/\\\.]/', '', $permalink);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $permalink. This often makes code more readable.
Loading history...
125
126 39
        if (in_array(fs::getExtension($permalink), self::$extensionsToStrip))
127 39
        {
128 4
            $permalink = fs::removeExtension($permalink);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $permalink. This often makes code more readable.
Loading history...
129 4
        }
130
131
        // Remove any special characters before a sane value
132 39
        $permalink = preg_replace('/^[^0-9a-zA-Z-_]*/', '', $permalink);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $permalink. This often makes code more readable.
Loading history...
133
134
        // Convert permalinks to lower case
135 39
        if (!Service::getParameter('build.preserveCase'))
136 39
        {
137 39
            $permalink = mb_strtolower($permalink, 'UTF-8');
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $permalink. This often makes code more readable.
Loading history...
138 39
        }
139
140 39
        return $permalink;
141
    }
142
}
143