InlineCss   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 8
c 4
b 1
f 0
lcom 0
cbo 3
dl 0
loc 49
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C process() 0 38 8
1
<?php
2
/**
3
 * Inliner for CSS
4
 *
5
 * @author  Tim Lochmüller
6
 */
7
8
namespace FRUIT\Ink\Postprocessing;
9
10
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
11
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
12
use TYPO3\CMS\Core\Utility\GeneralUtility;
13
use TYPO3\CMS\Core\Utility\HttpUtility;
14
use TYPO3\CMS\Extbase\Utility\DebuggerUtility;
15
16
/**
17
 * Inliner for CSS
18
 */
19
class InlineCss implements PostprocessingInterface
20
{
21
22
    /**
23
     * Call the inliner CSS processor
24
     *
25
     * @param string $content
26
     *
27
     * @return string
28
     */
29
    public function process($content)
30
    {
31
        GeneralUtility::requireOnce(ExtensionManagementUtility::extPath('ink', 'Resources/Private/Php/vendor/autoload.php'));
32
        $pattern = '%<(link|style)(?=[^<>]*?(?:type="(text/css)"|>))(?=[^<>]*?(?:media="([^<>"]*)"|>))(?=[^<>]*?(?:href="(.*?)"|>))(?=[^<>]*(?:rel="([^<>"]*)"|>))(?:.*?</\1>|[^<>]*>)%si';
33
        $matches = array();
34
        $css = '';
35
        preg_match_all($pattern, $content, $matches);
36
        if (isset($matches[0])) {
37
            foreach ($matches[0] as $key => $match) {
38
                if ($matches[1][$key] === 'style') {
39
                    $css .= strip_tags($match);
40
                } elseif (strpos($match, 'type="text/css"') !== false) {
41
                    $file = preg_replace('/^(.+)\.(\d+)\.css$/', '$1.css', $matches[4][$key]);
42
                    $parts = parse_url($file);
43
                    if (isset($parts['query'])) {
44
                        unset($parts['query']);
45
                    }
46
                    if (!isset($parts['host'])) {
47
                        $parts['path'] = ltrim($parts['path'], '/');
48
                    }
49
50
                    if ($parts['host'] === GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY')) {
51
                        unset($parts['scheme']);
52
                        unset($parts['host']);
53
                        $parts['path'] = ltrim($parts['path'], '/');
54
                    }
55
56
                    $file = HttpUtility::buildUrl($parts);
0 ignored issues
show
Security Bug introduced by
It seems like $parts defined by parse_url($file) on line 42 can also be of type false; however, TYPO3\CMS\Core\Utility\HttpUtility::buildUrl() does only seem to accept array, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
57
                    $css .= GeneralUtility::getUrl($file);
58
                } else {
59
                    continue;
60
                }
61
                $content = str_replace($match, '', $content);
62
            }
63
        }
64
        $format = new CssToInlineStyles($content, $css);
65
        return $format->convert();
66
    }
67
}
68