source/Parser/Url.php
Function parse
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
Open
protected function parse(string $url) {
$parsed = parse_url($url);
if ($parsed === false) {
$this->log("Failed to parse URL from {$url}");
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Avoid too many return
statements within this method. Open
Open
return $parsed;
Avoid using static access to class 't1gor\RobotsTxtParser\Parser\HostName' in method 'parse'. Open
Open
if (!isset($parsed['host']) || !HostName::isValid($parsed['host'])) {
- Read upRead up
- Exclude checks
StaticAccess
Since: 1.4.0
Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.
Example
class Foo
{
public function bar()
{
Bar::baz();
}
}
Source https://phpmd.org/rules/cleancode.html#staticaccess
Avoid variables with short names like $in. Configured minimum length is 3. Open
Open
protected string $in = '';
- Read upRead up
- Exclude checks
ShortVariable
Since: 0.2
Detects when a field, local, or parameter has a very short name.
Example
class Something {
private $q = 15; // VIOLATION - Field
public static function main( array $as ) { // VIOLATION - Formal
$r = 20 + $this->q; // VIOLATION - Local
for (int $i = 0; $i < 10; $i++) { // Not a Violation (inside FOR)
$r += $this->q;
}
}
}
Source https://phpmd.org/rules/naming.html#shortvariable
Avoid variables with short names like $in. Configured minimum length is 3. Open
Open
public function __construct(string $in) {
- Read upRead up
- Exclude checks
ShortVariable
Since: 0.2
Detects when a field, local, or parameter has a very short name.
Example
class Something {
private $q = 15; // VIOLATION - Field
public static function main( array $as ) { // VIOLATION - Formal
$r = 20 + $this->q; // VIOLATION - Local
for (int $i = 0; $i < 10; $i++) { // Not a Violation (inside FOR)
$r += $this->q;
}
}
}
Source https://phpmd.org/rules/naming.html#shortvariable
Opening brace of a class must be on the line after the definition Open
Open
class Url implements LoggerAwareInterface {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
',' => '!%2C!ui',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
* @param string $url
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
/**
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
*
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
'+' => '!%2B!ui',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
protected function parse(string $url) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
$parsed = parse_url($url);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
return $this->in;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
')' => '!%29!ui',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
'*' => '!%2A!ui',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
$this->log("Failed to parse URL from {$url}");
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
* @return string string
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
protected function encode(string $url): string {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
'=' => '!%3D!ui',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
'%' => '!%25!ui',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
return preg_replace(array_values($reserved), array_keys($reserved), rawurlencode($url));
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
$parsed = $this->parse($this->in);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
if ($parsed !== false) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
* @param string $url
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
']' => '!%5D!ui',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
/**
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
*
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
$this->in = $this->encode(trim($in));
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
'(' => '!%28!ui',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
*
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
public function getPath() {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
$reserved = [
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
'#' => '!%23!ui',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
'[' => '!%5B!ui',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
public static function isValidScheme(string $scheme): bool {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
if ($parsed === false) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
$this->log("URL scheme invalid or missing for {$url}");
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
return false;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
if (!isset($parsed['port'])) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
public function __construct(string $in) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
*
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
'$' => '!%24!ui',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
';' => '!%3B!ui',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
$parsed['port'] = getservbyname($parsed['scheme'], 'tcp');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
if (!is_int($parsed['port'])) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
return $parsed;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
'!' => '!%21!ui',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
"'" => '!%27!ui',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
* Returns a string containing the encoded URL with disallowed characters converted to their percentage encodings.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
'/' => '!%2F!ui',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
*/
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
protected string $in = '';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
'&' => '!%26!ui',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
return false;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
* URL encoder according to RFC 3986
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
*
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
'?' => '!%3F!ui',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
return in_array($scheme, ['http', 'https', 'ftp', 'sftp']);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
* Parse URL
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
*/
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
':' => '!%3A!ui',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
$this->log("URL host invalid or missing for {$url}");
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
* @link http://publicmind.in/blog/url-encoding/
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
'@' => '!%40!ui',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
return false;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
if (!isset($parsed['host']) || !HostName::isValid($parsed['host'])) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
$parsed['custom'] = ($parsed['path'] ?? '/') . (isset($parsed['query']) ? '?' . $parsed['query'] : '');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
use LogsIfAvailableTrait;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
* @return array|false
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
if (!isset($parsed['scheme']) || !static::isValidScheme($parsed['scheme'])) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
return false;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
$this->log("URL port should be a number, {$parsed['port']} found for {$url}");
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
return $parsed['custom'];
- Exclude checks
Opening brace should be on a new line Open
Open
protected function encode(string $url): string {
- Exclude checks
Opening brace should be on a new line Open
Open
protected function parse(string $url) {
- Exclude checks
Opening brace should be on a new line Open
Open
public static function isValidScheme(string $scheme): bool {
- Exclude checks
Opening brace should be on a new line Open
Open
public function getPath() {
- Exclude checks
Opening brace should be on a new line Open
Open
public function __construct(string $in) {
- Exclude checks