Completed
Pull Request — master (#48)
by Vladimir
02:47
created

PermalinkDocument::getTargetFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
crap 2
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
abstract class PermalinkDocument extends ReadableDocument
0 ignored issues
show
Coding Style introduced by
PermalinkDocument does not seem to conform to the naming convention (^Abstract|Factory$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
11
{
12
    /** @var array */
13
    protected $permalink;
14
15
    /** @var array */
16
    protected $redirects;
17
18
    /**
19
     * Get the destination of where this Content Item would be written to when the website is compiled.
20
     *
21
     * @return string
22
     */
23 20
    final public function getTargetFile()
24
    {
25 20
        $permalink = $this->getPermalink();
26 20
        $missingFile = (substr($permalink, -1) == '/');
27 20
        $permalink = str_replace('/', DIRECTORY_SEPARATOR, $permalink);
28
29
        if ($missingFile)
30 20
        {
31 12
            $permalink = rtrim($permalink, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'index.html';
32 12
        }
33
34 20
        return ltrim($permalink, DIRECTORY_SEPARATOR);
35
    }
36
37
    /**
38
     * Get the permalink of this Content Item.
39
     *
40
     * @throws \Exception
41
     *
42
     * @return string
43
     */
44 39
    final public function getPermalink()
45
    {
46 39
        $this->buildPermalink();
47
48 39
        $this->permalink = $this->sanitizePermalink($this->permalink);
0 ignored issues
show
Documentation introduced by
$this->permalink is of type array, but the function expects a string.

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...
Documentation Bug introduced by
It seems like $this->sanitizePermalink($this->permalink) of type string is incompatible with the declared type array of property $permalink.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
49 39
        $this->permalink = str_replace(DIRECTORY_SEPARATOR, '/', $this->permalink);
0 ignored issues
show
Documentation Bug introduced by
It seems like str_replace(DIRECTORY_SE... '/', $this->permalink) of type string is incompatible with the declared type array of property $permalink.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
50 39
        $this->permalink = '/' . ltrim($this->permalink, '/'); // Permalinks should always use '/' and not be OS specific
0 ignored issues
show
Documentation Bug introduced by
It seems like '/' . ltrim($this->permalink, '/') of type string is incompatible with the declared type array of property $permalink.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
51
52 39
        return $this->permalink;
53
    }
54
55
    /**
56
     * Get an array of URLs that will redirect to.
57
     *
58
     * @return string[]
0 ignored issues
show
Documentation introduced by
Should the return type not be null|array?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
59
     */
60 14
    final public function getRedirects()
61
    {
62 14
        if (is_null($this->redirects))
63 14
        {
64 3
            $this->getPermalink();
65 3
        }
66
67 14
        return $this->redirects;
68
    }
69
70
    /**
71
     * Get the permalink based off the location of where the file is relative to the website. This permalink is to be
72
     * used as a fallback in the case that a permalink is not explicitly specified in the Front Matter.
73
     *
74
     * @return string
75
     */
76 3
    protected function getPathPermalink()
77
    {
78
        // Remove the protocol of the path, if there is one and prepend a '/' to the beginning
79 3
        $cleanPath = preg_replace('/[\w|\d]+:\/\//', '', $this->getRelativeFilePath());
80 3
        $cleanPath = ltrim($cleanPath, DIRECTORY_SEPARATOR);
81
82
        // Handle vfs:// paths by replacing their forward slashes with the OS appropriate directory separator
83 3
        if (DIRECTORY_SEPARATOR !== '/')
84 3
        {
85
            $cleanPath = str_replace('/', DIRECTORY_SEPARATOR, $cleanPath);
86
        }
87
88
        // Check the first folder and see if it's a data folder (starts with an underscore) intended for stakx
89 3
        $folders = explode(DIRECTORY_SEPARATOR, $cleanPath);
90
91 3
        if (substr($folders[0], 0, 1) === '_')
92 3
        {
93 1
            array_shift($folders);
94 1
        }
95
96 3
        $cleanPath = implode(DIRECTORY_SEPARATOR, $folders);
97
98 3
        return $cleanPath;
99
    }
100
101
    /**
102
     * Sanitize a permalink to remove unsupported characters or multiple '/' and replace spaces with hyphens.
103
     *
104
     * @param string $permalink A permalink
105
     *
106
     * @return string $permalink The sanitized permalink
107
     */
108 39
    protected function sanitizePermalink($permalink)
109
    {
110
        // Remove multiple '/' together
111 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...
112
113
        // Replace all spaces with hyphens
114 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...
115
116
        // Remove all disallowed characters
117 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...
118
119
        // Handle unnecessary extensions
120 39
        $extensionsToStrip = array('twig');
121
122 39
        if (in_array($this->fs->getExtension($permalink), $extensionsToStrip))
123 39
        {
124 4
            $permalink = $this->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...
125 4
        }
126
127
        // Remove any special characters before a sane value
128 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...
129
130
        // Convert permalinks to lower case
131 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...
132
133 39
        return $permalink;
134
    }
135
136
    /**
137
     * Evaluate the FrontMatter for the document.
138
     *
139
     * This FrontMatter can be the user-defined FrontMatter in a FrontMatterDocument but it can also be used as internal
140
     * settings used for objects that do not have user-defined FrontMatter such as DataItems.
141
     *
142
     * @param array $variables
143
     *
144
     * @return void
145
     */
146
    abstract public function evaluateFrontMatter($variables = array());
147
148
    /**
149
     * @return void
150
     */
151
    abstract protected function buildPermalink();
152
}
153