Issues (19)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Minify.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace ArjanSchouten\HtmlMinifier;
4
5
use ArjanSchouten\HtmlMinifier\Minifiers\Html\AttributeQuoteMinifier;
6
use ArjanSchouten\HtmlMinifier\Minifiers\Html\BooleanAttributeMinifier;
7
use ArjanSchouten\HtmlMinifier\Minifiers\Html\CommentMinifier;
8
use ArjanSchouten\HtmlMinifier\Minifiers\Html\EmptyAttributeMinifier;
9
use ArjanSchouten\HtmlMinifier\Minifiers\Html\JavascriptEventsMinifier;
10
use ArjanSchouten\HtmlMinifier\Minifiers\Html\OptionalElementMinifier;
11
use ArjanSchouten\HtmlMinifier\Minifiers\Html\RedundantAttributeMinifier;
12
use ArjanSchouten\HtmlMinifier\Minifiers\Html\WhitespaceMinifier;
13
use ArjanSchouten\HtmlMinifier\Minifiers\MinifierInterface;
14
use ArjanSchouten\HtmlMinifier\Placeholders\CommentPlaceholder;
15
use ArjanSchouten\HtmlMinifier\Placeholders\Php\PhpPlaceholder;
16
use ArjanSchouten\HtmlMinifier\Placeholders\PlaceholderInterface;
17
use ArjanSchouten\HtmlMinifier\Placeholders\WhitespacePlaceholder;
18
use InvalidArgumentException;
19
20
class Minify
21
{
22
    /**
23
     * @var \ArjanSchouten\HtmlMinifier\Placeholders\PlaceholderInterface[]
24
     */
25
    protected $placeholders = [
26
        PhpPlaceholder::class,
27
        CommentPlaceholder::class,
28
        WhitespacePlaceholder::class,
29
    ];
30
31
    /**
32
     * @var \ArjanSchouten\HtmlMinifier\Minifiers\MinifierInterface[]
33
     */
34
    protected $minifiers = [
35
        CommentMinifier::class,
36
        WhitespaceMinifier::class,
37
        AttributeQuoteMinifier::class,
38
        EmptyAttributeMinifier::class,
39
        OptionalElementMinifier::class,
40
        BooleanAttributeMinifier::class,
41
        JavascriptEventsMinifier::class,
42
        RedundantAttributeMinifier::class,
43
    ];
44
45
    /**
46
     * @param \ArjanSchouten\HtmlMinifier\MinifyContext $context
47
     * @param array                                     $options
48
     *
49
     * @return \ArjanSchouten\HtmlMinifier\MinifyContext
50
     */
51 3
    public function run(MinifyContext $context, $options = [])
52
    {
53 3
        $context->addMinificationStep($context->getContents(), 'Initial step');
54 3
        $context = $this->placeholders($context);
55 3
        $context = $this->minifiers($context, $options);
56
57 3
        return $this->restore($context);
58
    }
59
60
    /**
61
     * @param \ArjanSchouten\HtmlMinifier\MinifyContext $context
62
     *
63
     * @return \ArjanSchouten\HtmlMinifier\MinifyContext
64
     */
65 3
    protected function placeholders(MinifyContext $context)
66
    {
67 3
        foreach ($this->placeholders as $placeholder) {
68 3
            $placeholder = new $placeholder();
69 3
            $context = $placeholder->process($context);
70
        }
71
72 3
        return $context;
73
    }
74
75
    /**
76
     * @param \ArjanSchouten\HtmlMinifier\MinifyContext $context
77
     * @param array                                     $options
78
     *
79
     * @return \ArjanSchouten\HtmlMinifier\MinifyContext
80
     */
81 3
    protected function minifiers(MinifyContext $context, $options = [])
82
    {
83 3
        foreach ($this->minifiers as $minifier) {
84 3
            $minifier = new $minifier();
85
86 3
            $provides = $minifier->provides();
87 3
            if ($this->runAll($options) || $this->isOptionSet($provides, $options)) {
88 3
                $context = $minifier->process($context);
89 3
                $context->addMinificationStep($context->getContents(), $this->getClassName($minifier).'|'.$minifier->provides());
90
            }
91
        }
92
93 3
        return $context;
94
    }
95
96
    /**
97
     * Checks if all minifiers should be runned.
98
     *
99
     * @param array $options
100
     *
101
     * @return bool
102
     */
103 3
    protected function runAll($options = [])
104
    {
105 3
        return isset($options[Options::ALL]) && $options[Options::ALL];
106
    }
107
108
    /**
109
     * Check whether an option is set in the options aray.
110
     *
111
     * @param string $provides
112
     * @param array  $options
113
     *
114
     * @return bool
115
     */
116 2
    protected function isOptionSet($provides, $options = [])
117
    {
118 2
        if (isset($options[$provides]) && $options[$provides] === true) {
119 2
            return true;
120 2
        } elseif (!isset($options[$provides]) && Options::options()[$provides]->isDefault()) {
121 2
            return true;
122
        }
123
124 2
        return false;
125
    }
126
127
    /**
128
     * Restore placeholders with their original content.
129
     *
130
     * @param \ArjanSchouten\HtmlMinifier\MinifyContext $context
131
     *
132
     * @return \ArjanSchouten\HtmlMinifier\MinifyContext
133
     */
134 3
    protected function restore(MinifyContext $context)
135
    {
136 3
        $withoutPlaceholders = $context->getPlaceholderContainer()->restorePlaceholders($context->getContents());
137
138 3
        return $context->setContents($withoutPlaceholders);
139
    }
140
141
    /**
142
     * @return \ArjanSchouten\HtmlMinifier\Placeholders\PlaceholderInterface[]
143
     */
144 1
    public function getPlaceholders()
145
    {
146 1
        return $this->placeholders;
147
    }
148
149
    /**
150
     * Add a placeholder strategy to the registered placeholders.
151
     *
152
     * @param string $placeholder
153
     *
154
     * @return \ArjanSchouten\HtmlMinifier\Placeholders\PlaceholderInterface[]
0 ignored issues
show
Should the return type not be array<PlaceholderInterface|string>?

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...
155
     */
156 1 View Code Duplication
    public function addPlaceholder($placeholder)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
157
    {
158 1
        if (!isset(class_implements($placeholder)[PlaceholderInterface::class])) {
159 1
            throw new InvalidArgumentException('The class ['.$placeholder.'] should be a member of the ['.PlaceholderInterface::class.']');
160 1
        } elseif (in_array($placeholder, $this->placeholders)) {
161
            throw new InvalidArgumentException('The placeholder ['.$placeholder.'] is already added to the minifier!');
162
        }
163
164 1
        $this->placeholders[] = $placeholder;
165
166 1
        return $this->placeholders;
167
    }
168
169
    /**
170
     * @return \ArjanSchouten\HtmlMinifier\Minifiers\MinifierInterface[]
171
     */
172 1
    public function getMinifiers()
173
    {
174 1
        return $this->minifiers;
175
    }
176
177
    /**
178
     * Add a placeholder strategy to the registered placeholders.
179
     *
180
     * @param string $minifier
181
     *
182
     * @return \ArjanSchouten\HtmlMinifier\Minifiers\MinifierInterface[]
0 ignored issues
show
Should the return type not be array<MinifierInterface|string>?

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...
183
     */
184 1 View Code Duplication
    public function addMinifier($minifier)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
185
    {
186 1
        if (!isset(class_implements($minifier)[MinifierInterface::class])) {
187
            throw new InvalidArgumentException('The class ['.$minifier.'] should be a member of the ['.MinifierInterface::class.']');
188 1
        } elseif (in_array($minifier, $this->minifiers)) {
189
            throw new InvalidArgumentException('The minifier ['.$minifier.'] is already added to the minifier!');
190
        }
191
192 1
        $this->minifiers[] = $minifier;
193
194 1
        return $this->minifiers;
195
    }
196
197
    /**
198
     * Get the classname without the namespace.
199
     *
200
     * @param $object
201
     *
202
     * @return string
203
     */
204 3
    private function getClassName($object)
205
    {
206 3
        $class = get_class($object);
207 3
        if (($index = strrpos($class, '\\'))) {
208 3
            $fixForTrailingBackslash = 1;
209
210 3
            return substr($class, $index + $fixForTrailingBackslash);
211
        }
212
213
        return $class;
214
    }
215
}
216