Showing 24 of 24 total issues
The class Topic has an overall complexity of 69 which is very high. The configured complexity threshold is 50. Open
class Topic extends Base
{
const ERROR_NOT_FOUND = 'Topic Not Found';
/** @var array $data - topic data */
- Exclude checks
Function setNamespaces
has a Cognitive Complexity of 21 (exceeds 5 allowed). Consider refactoring. Open
private function setNamespaces()
{
foreach ($this->data[self::TOPICS] as $topic) {
if (!isset($topic[self::EXISTS])) {
$this->vars[self::MISSING][] = $topic[self::ASTERISK]; // page does not exist
- 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
Method setNamespaces
has 57 lines of code (exceeds 25 allowed). Consider refactoring. Open
private function setNamespaces()
{
foreach ($this->data[self::TOPICS] as $topic) {
if (!isset($topic[self::EXISTS])) {
$this->vars[self::MISSING][] = $topic[self::ASTERISK]; // page does not exist
File Topic.php
has 259 lines of code (exceeds 250 allowed). Consider refactoring. Open
<?php
/**
* Just Refs - https://github.com/attogram/justrefs
* Topic Class
*/
Function listify
has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring. Open
private function listify($index)
{
if (in_array($index, [self::EXISTS, self::MISSING]) // skip internal-usage vars
|| empty($this->vars[$index]) // Error - index not found, or index empty
) {
- 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
Function removeTemplateTopics
has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring. Open
private function removeTemplateTopics()
{
if (empty($this->vars[self::MAIN])
|| (empty($this->vars[self::TEMPLATE]) && $this->vars[self::TEMPLATE_SECONDARY])
) {
- 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
Function setTemplates
has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring. Open
private function setTemplates()
{
foreach ($this->data[self::TEMPLATES] as $item) {
switch ($item[self::NS]) {
case '0': // Main
- 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
Method links
has 26 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function links($query)
{
$data = $this->getApi($this->api . $this->apiLinks . urlencode($query));
if (!$data || !is_array($data)) {
$this->error('links: decode failed: ' . $query);
Function match
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
private function match($match)
{
switch ($match) {
case self::TOPIC:
(new Topic($this->verbose, $this->router, $this->template))->get();
- 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
Function get
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
public function get()
{
if (!$this->setTopicFromUrl()) { // build topic from URL
$this->error404(self::ERROR_NOT_FOUND);
}
- 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
Remove error control operator '@' on line 130. Open
private function getApi($url)
{
if (empty($url) || !is_string($url)) {
$this->error('getApi: invalid url: ' . print_r($url, true));
return false;
- Read upRead up
- Exclude checks
ErrorControlOperator
Error suppression should be avoided if possible as it doesn't just suppress the error, that you are trying to stop, but will also suppress errors that you didn't predict would ever occur. Consider changing error_reporting() level and/or setting up your own error handler.
Example
function foo($filePath) {
$file = @fopen($filPath); // hides exceptions
$key = @$array[$notExistingKey]; // assigns null to $key
}
Source http://phpmd.org/rules/cleancode.html#errorcontroloperator
Missing class import via use statement (line '163', column '21'). Open
return (new \DateTime())->format('u') . ': ' . get_class($this)
- Read upRead up
- Exclude checks
MissingImport
Since: 2.7.0
Importing all external classes in a file through use statements makes them clearly visible.
Example
function make() {
return new \stdClass();
}
Source http://phpmd.org/rules/cleancode.html#MissingImport
The method __construct has a boolean flag argument $verbose, which is a certain sign of a Single Responsibility Principle violation. Open
public function __construct($verbose = false, $router = null, $template = null)
- Read upRead up
- Exclude checks
BooleanArgumentFlag
Since: 1.4.0
A boolean flag argument is a reliable indicator for a violation of the Single Responsibility Principle (SRP). You can fix this problem by extracting the logic in the boolean flag into its own class or method.
Example
class Foo {
public function bar($flag = true) {
}
}
Source https://phpmd.org/rules/cleancode.html#booleanargumentflag
Duplicated array key , first declared at line 188. Open
$replacers = [
' ' => '_',
'%' => '%25', // do first before any other %## replacers
'"' => '%22',
'&' => '%26',
- Read upRead up
- Exclude checks
DuplicatedArrayKey
Defining another value for the same key in an array literal overrides the previous key/value, which makes it effectively an unused code. If it's known from the beginning that the key will have different value, there is usually no point in defining first one.
Example
function createArray() {
return [
'non-associative 0element', // not applied
0 => 'associative 0-element', // applied
false => 'associative 0-element', // applied
'foo' => 'bar', // not applied
"foo" => 'baz', // applied
];
}
Source http://phpmd.org/rules/cleancode.html#duplicatedarraykey
Function search
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
public function search($query)
{
$data = $this->getApi($this->api . $this->apiSearch . urlencode($query));
if (!$data || !is_array($data) || empty($data[self::QUERY])
|| empty($data[self::QUERY][self::SEARCH]) || !is_array($data[self::QUERY][self::SEARCH])
- 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
The method error404() contains an exit expression. Open
exit;
- Read upRead up
- Exclude checks
ExitExpression
Since: 0.2
An exit-expression within regular code is untestable and therefore it should be avoided. Consider to move the exit-expression into some kind of startup script where an error/exception code is returned to the calling environment.
Example
class Foo {
public function bar($param) {
if ($param === 42) {
exit(23);
}
}
}
Source https://phpmd.org/rules/design.html#exitexpression
The method Mediawiki::getApi() calls the typical debug function print_r() which is mostly only used during development. Open
$this->error('getApi: invalid url: ' . print_r($url, true));
- Read upRead up
- Exclude checks
DevelopmentCodeFragment
Since: 2.3.0
Functions like vardump(), printr() etc. are normally only used during development and therefore such calls in production code are a good indicator that they were just forgotten.
Example
class SuspectCode {
public function doSomething(array $items)
{
foreach ($items as $i => $item) {
// …
if ('qafoo' == $item) var_dump($i);
// …
}
}
}
Source https://phpmd.org/rules/design.html#developmentcodefragment
The method setNamespaces() has a Cyclomatic Complexity of 19. The configured cyclomatic complexity threshold is 10. Open
private function setNamespaces()
{
foreach ($this->data[self::TOPICS] as $topic) {
if (!isset($topic[self::EXISTS])) {
$this->vars[self::MISSING][] = $topic[self::ASTERISK]; // page does not exist
- Read upRead up
- Exclude checks
CyclomaticComplexity
Since: 0.1
Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.
Example
// Cyclomatic Complexity = 11
class Foo {
1 public function example() {
2 if ($a == $b) {
3 if ($a1 == $b1) {
fiddle();
4 } elseif ($a2 == $b2) {
fiddle();
} else {
fiddle();
}
5 } elseif ($c == $d) {
6 while ($c == $d) {
fiddle();
}
7 } elseif ($e == $f) {
8 for ($n = 0; $n < $h; $n++) {
fiddle();
}
} else {
switch ($z) {
9 case 1:
fiddle();
break;
10 case 2:
fiddle();
break;
11 case 3:
fiddle();
break;
default:
fiddle();
break;
}
}
}
}
Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity
The method Base::formatMessage() calls the typical debug function print_r() which is mostly only used during development. Open
. ': ' . htmlentities(print_r($message, true));
- Read upRead up
- Exclude checks
DevelopmentCodeFragment
Since: 2.3.0
Functions like vardump(), printr() etc. are normally only used during development and therefore such calls in production code are a good indicator that they were just forgotten.
Example
class SuspectCode {
public function doSomething(array $items)
{
foreach ($items as $i => $item) {
// …
if ('qafoo' == $item) var_dump($i);
// …
}
}
}
Source https://phpmd.org/rules/design.html#developmentcodefragment
Reduce the number of returns of this function 4, down to the maximum allowed 3. Open
private function getApi($url)
- Read upRead up
- Exclude checks
Having too many return statements in a function increases the function's essential complexity because the flow of execution is broken each time a return statement is encountered. This makes it harder to read and understand the logic of the function.
Noncompliant Code Example
With the default threshold of 3:
function myFunction(){ // Noncompliant as there are 4 return statements if (condition1) { return true; } else { if (condition2) { return false; } else { return true; } } return false; }