GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( d56282...b11258 )
by Leonardo
02:50
created

addClasses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 9
ccs 0
cts 6
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQLAPI\GraphQLAPI\ModuleResolvers;
6
7
use Parsedown;
8
9
trait HasMarkdownDocumentationModuleResolverTrait
10
{
11
    /**
12
     * The module slug
13
     */
14
    abstract public function getSlug(string $module): string;
15
16
    /**
17
     * The name of the Markdown filename.
18
     * By default, it's the same as the slug
19
     *
20
     * @param string $module
21
     * @return string
22
     */
23
    public function getMarkdownFilename(string $module): ?string
24
    {
25
        return $this->getSlug($module) . '.md';
26
    }
27
28
    /**
29
     * Where the markdown file localized to the user's language is stored
30
     *
31
     * @param string $module
32
     * @return string
33
     */
34
    abstract public function getLocalizedMarkdownFileDir(string $module): string;
35
36
    /**
37
     * Where the default markdown file (for if the localized language is not available) is stored
38
     *
39
     * @param string $module
40
     * @return string
41
     */
42
    abstract public function getDefaultMarkdownFileDir(string $module): string;
43
44
    /**
45
     * Does the module have HTML Documentation?
46
     *
47
     * @param string $module
48
     * @return bool
49
     */
50
    public function hasDocumentation(string $module): bool
51
    {
52
        return !empty($this->getMarkdownFilename($module));
53
    }
54
55
    /**
56
     * Path URL to append to the local images referenced in the markdown file
57
     *
58
     * @param string $module
59
     * @return string|null
60
     */
61
    abstract protected function getDefaultMarkdownFileURL(string $module): string;
62
63
    /**
64
     * HTML Documentation for the module
65
     *
66
     * @param string $module
67
     * @return string|null
68
     */
69
    public function getDocumentation(string $module): ?string
70
    {
71
        if ($markdownFilename = $this->getMarkdownFilename($module)) {
72
            $localizedMarkdownFile = \trailingslashit($this->getLocalizedMarkdownFileDir($module)) . $markdownFilename;
73
            if (file_exists($localizedMarkdownFile)) {
74
                // First check if the localized version exists
75
                $markdownFile = $localizedMarkdownFile;
76
            } else {
77
                // Otherwise, use the default language version
78
                $markdownFile = \trailingslashit($this->getDefaultMarkdownFileDir($module)) . $markdownFilename;
79
                // Make sure this file exists
80
                if (!file_exists($markdownFile)) {
81
                    return sprintf(
82
                        '<p>%s</p>',
83
                        \__('Oops, the documentation for this module is not available', 'graphql-api')
84
                    );
85
                }
86
            }
87
            $markdownContents = file_get_contents($markdownFile);
88
            $htmlContents = (new Parsedown())->text($markdownContents);
89
            $defaultModulePathURL = $this->getDefaultMarkdownFileURL($module);
90
            // Add the path to the images and anchors
91
            $htmlContents = $this->appendPathURLToImages($defaultModulePathURL, $htmlContents);
92
            $htmlContents = $this->appendPathURLToAnchors($defaultModulePathURL, $htmlContents);
93
            // Add classes to HTML elements
94
            $htmlContents = $this->addClasses($htmlContents);
95
            return $htmlContents;
96
        }
97
        return null;
98
    }
99
100
    /**
101
     * Add classes to the HTML elements
102
     */
103
    protected function addClasses(string $htmlContents): string
104
    {
105
        /**
106
         * Add class "wp-list-table widefat" to all tables
107
         */
108
        return str_replace(
109
            '<table>',
110
            '<table class="wp-list-table widefat">',
111
            $htmlContents
112
        );
113
    }
114
115
    /**
116
     * Convert relative paths to absolute paths for image URLs
117
     *
118
     * @param string $pathURL
119
     * @param string $htmlContents
120
     * @return string
121
     */
122
    protected function appendPathURLToImages(string $pathURL, string $htmlContents): string
123
    {
124
        return $this->appendPathURLToElement('img', 'src', $pathURL, $htmlContents);
125
    }
126
127
    /**
128
     * Convert relative paths to absolute paths for image URLs
129
     *
130
     * @param string $pathURL
131
     * @param string $htmlContents
132
     * @return string
133
     */
134
    protected function appendPathURLToAnchors(string $pathURL, string $htmlContents): string
135
    {
136
        return $this->appendPathURLToElement('a', 'href', $pathURL, $htmlContents);
137
    }
138
139
    /**
140
     * Convert relative paths to absolute paths for elements
141
     *
142
     * @param string $tag
143
     * @param string $attr
144
     * @param string $pathURL
145
     * @param string $htmlContents
146
     * @return string
147
     */
148
    protected function appendPathURLToElement(string $tag, string $attr, string $pathURL, string $htmlContents): string
149
    {
150
        /**
151
         * $regex will become:
152
         * - /<img.*src="(.*?)".*?>/
153
         * - /<a.*href="(.*?)".*?>/
154
         */
155
        $regex = sprintf(
156
            '/<%s.*%s="(.*?)".*?>/',
157
            $tag,
158
            $attr
159
        );
160
        return preg_replace_callback(
161
            $regex,
162
            function ($matches) use ($pathURL, $attr) {
163
                // If the element has an absolute route, then no need
164
                if (substr($matches[1], 0, strlen('http://')) == 'http://'
165
                    || substr($matches[1], 0, strlen('https://')) == 'https://'
166
                ) {
167
                    return $matches[0];
168
                }
169
                $elementURL = \trailingslashit($pathURL) . $matches[1];
170
                return str_replace(
171
                    "{$attr}=\"{$matches[1]}\"",
172
                    "{$attr}=\"{$elementURL}\"",
173
                    $matches[0]
174
                );
175
            },
176
            $htmlContents
177
        );
178
    }
179
}
180