Function compileSource
has a Cognitive Complexity of 23 (exceeds 5 allowed). Consider refactoring. Open
public function compileSource(Source $source): string
{
$path = $source->getPath();
if ($this->pugSymfonyEngine->supports($path)) {
- 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 compileSource
has 45 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function compileSource(Source $source): string
{
$path = $source->getPath();
if ($this->pugSymfonyEngine->supports($path)) {
Method fromTwigEnvironment
has 27 lines of code (exceeds 25 allowed). Consider refactoring. Open
public static function fromTwigEnvironment(TwigEnvironment $baseTwig, PugSymfonyEngine $pugSymfonyEngine, ContainerInterface $container)
{
$twig = new static($baseTwig->getLoader(), [
'debug' => $baseTwig->isDebug(),
'charset' => $baseTwig->getCharset(),
Avoid deeply nested control flow statements. Open
if ($newLine > $templateLine) {
$templateLine = $newLine;
$debugInfo[$codeFirstLine + $index] = $newLine;
}
The method compileSource() has a Cyclomatic Complexity of 11. The configured cyclomatic complexity threshold is 10. Open
public function compileSource(Source $source): string
{
$path = $source->getPath();
if ($this->pugSymfonyEngine->supports($path)) {
- 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
Refactor this function to reduce its Cognitive Complexity from 29 to the 15 allowed. Open
public function compileSource(Source $source): string
- Read upRead up
- Exclude checks
Cognitive Complexity is a measure of how hard the control flow of a function is to understand. Functions with high Cognitive Complexity will be difficult to maintain.
See
Avoid using undefined variables such as '$arguments' which will lead to PHP notices. Open
$arguments[] = $name;
- Read upRead up
- Exclude checks
UndefinedVariable
Since: 2.8.0
Detects when a variable is used that has not been defined before.
Example
class Foo
{
private function bar()
{
// $message is undefined
echo $message;
}
}
Source https://phpmd.org/rules/cleancode.html#undefinedvariable
Duplicated array key {{filename}}, first declared at line 177. Open
$replacements = [
$fileName => $className,
'"{{filename}}"' => var_export($name, true),
'{{filename}}' => $name,
'"{{path}}"' => var_export($path, true),
- 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
Define and throw a dedicated exception instead of using a generic one. Open
throw new RuntimeException('Unable to compile '.$name.' function.');
- Read upRead up
- Exclude checks
If you throw a general exception type, such as ErrorException, RuntimeException, or Exception in a library or framework, it forces consumers to catch all exceptions, including unknown exceptions that they do not know how to handle.
Instead, either throw a subtype that already exists in the Standard PHP Library, or create your own type that derives from Exception.
Noncompliant Code Example
throw new Exception(); // Noncompliant
Compliant Solution
throw new InvalidArgumentException(); // or throw new UnexpectedValueException();
See
- MITRE, CWE-397 - Declaration of Throws for Generic Exception
- CERT, ERR07-J. - Do not throw RuntimeException, Exception, or Throwable
Avoid unused local variables such as '$arguments'. Open
$arguments[] = $name;
- Read upRead up
- Exclude checks
UnusedLocalVariable
Since: 0.2
Detects when a local variable is declared and/or assigned, but not used.
Example
class Foo {
public function doSomething()
{
$i = 5; // Unused
}
}
Source https://phpmd.org/rules/unusedcode.html#unusedlocalvariable
Call to undeclared method \Pug\Twig\Environment::compile
Open
$output = $this->compile($parser->parse($stream));
- Exclude checks
Catching undeclared class \Twig\Error\RuntimeError
Open
} catch (RuntimeError $error) {
- Exclude checks
Call to method getFilters
from undeclared class \Twig\Environment
(Did you mean class \Pug\Twig\Environment) Open
foreach ($baseTwig->getFilters() as $filter) {
- Exclude checks
Reference to undeclared class \Twig\Environment
(Did you mean class \Pug\Twig\Environment) Open
$html = parent::compileSource($source);
- Exclude checks
Call to method getFunctions
from undeclared class \Twig\Environment
(Did you mean class \Pug\Twig\Environment) Open
foreach ($baseTwig->getFunctions() as $function) {
- Exclude checks
Parameter $function
has undeclared type \Twig\TwigFunction
Open
public function compileCode(TwigFunction $function, string $code)
- Exclude checks
Call to method getName
from undeclared class \Twig\Source
Open
$name = $source->getName();
- Exclude checks
Call to method getExtensions
from undeclared class \Twig\Environment
(Did you mean class \Pug\Twig\Environment) Open
$extensions = $baseTwig->getExtensions();
- Exclude checks
Reference to undeclared class \Twig\Environment
(Did you mean class \Pug\Twig\Environment) Open
return parent::getTemplateClass($name, $index);
- Exclude checks
Parameter $baseTwig
has undeclared type \Twig\Environment
(Did you mean class \Pug\Twig\Environment) Open
public static function fromTwigEnvironment(TwigEnvironment $baseTwig, PugSymfonyEngine $pugSymfonyEngine, ContainerInterface $container)
- Exclude checks
Variable $arguments
was undeclared, but array fields are being added to it. Open
$arguments[] = $name;
- Exclude checks
Reference to constant class
from undeclared class \Twig\Extension\OptimizerExtension
Open
'optimizations' => static::getPrivateExtensionProperty($baseTwig, OptimizerExtension::class, 'optimizers'),
- Exclude checks
Call to method parse
from undeclared class \Twig\Parser
Open
$output = $this->compile($parser->parse($stream));
- Exclude checks
Call to method getExtension
from undeclared class \Twig\Environment
(Did you mean class \Pug\Twig\Environment) Open
return static::getPrivateProperty($twig->getExtension($extension), $property);
- Exclude checks
Call to undeclared method \Pug\Twig\Environment::isDebug
Open
if ($this->isDebug()) {
- Exclude checks
Call to undeclared method \Pug\Twig\Environment::isDebug
Open
$codeFirstLine = $this->isDebug() ? 31 : 25;
- Exclude checks
Class extends undeclared class \Twig\Environment
(Did you mean class \Pug\Twig\Environment) Open
class Environment extends TwigEnvironment
- Exclude checks
Call to method getCode
from undeclared class \Twig\Source
Open
$code = $source->getCode();
- Exclude checks
Parameter $loader
has undeclared type \Twig\Loader\LoaderInterface
Open
public function __construct(LoaderInterface $loader, $options = [])
- Exclude checks
Reference to constant class
from undeclared class \Twig\Extension\EscaperExtension
Open
'autoescape' => static::getPrivateExtensionProperty($baseTwig, EscaperExtension::class, 'defaultStrategy'),
- Exclude checks
Call to method getName
from undeclared class \Twig\TwigFunction
Open
$name = $function->getName();
- Exclude checks
Call to method getGlobals
from undeclared class \Twig\Environment
(Did you mean class \Pug\Twig\Environment) Open
foreach ($baseTwig->getGlobals() as $key => $value) {
- Exclude checks
Call to method getCompiler
from undeclared class \Pug\Pug
Open
$formatter = $pug->getCompiler()->getFormatter();
- Exclude checks
Reference to undeclared class \Twig\Environment
(Did you mean class \Pug\Twig\Environment) Open
parent::__construct($loader, $options);
- Exclude checks
Call to undeclared method \Pug\Twig\Environment::isDebug
Open
$fileName = $this->isDebug() ? 'PugDebugTemplateTemplate' : 'PugTemplateTemplate';
- Exclude checks
Property \Pug\Twig\Environment->container
has undeclared type \Symfony\Component\DependencyInjection\ContainerInterface
Open
protected $container;
- Exclude checks
Call to method __construct
from undeclared class \Twig\Source
Open
$stream = $this->tokenize(new Source($code, $path, $path));
- Exclude checks
Reference to undeclared class \Twig\Environment
(Did you mean class \Pug\Twig\Environment) Open
return parent::render($name, $context);
- Exclude checks
Call to method getPath
from undeclared class \Twig\Source
Open
$path = $source->getPath();
- Exclude checks
Call to undeclared method \Pug\Twig\Environment::addGlobal
Open
$twig->addGlobal($key, $value);
- Exclude checks
Call to undeclared method \Pug\Twig\Environment::isDebug
Open
if ($this->isDebug()) {
- Exclude checks
Call to undeclared method \Pug\Twig\Environment::addFunction
Open
$twig->addFunction($function);
- Exclude checks
Call to undeclared method \Pug\Twig\Environment::getExtensions
Open
foreach (array_keys($twig->getExtensions()) as $key) {
- Exclude checks
Call to method compile
from undeclared class \Pug\Pug
Open
$php = $pug->compile($code, $path);
- Exclude checks
Call to method getRuntime
from undeclared class \Twig\Environment
(Did you mean class \Pug\Twig\Environment) Open
return $this->rootEnv->getRuntime($class);
- Exclude checks
Reference to undeclared class \Twig\Environment
(Did you mean class \Pug\Twig\Environment) Open
return parent::getRuntime($class);
- Exclude checks
Return type of loadTemplate()
is undeclared type \Twig\Template
Open
public function loadTemplate(string $cls, string $name, int $index = null): Template
- Exclude checks
Reference to undeclared class \Twig\Environment
(Did you mean class \Pug\Twig\Environment) Open
return parent::loadTemplate($cls, $name, $index);
- Exclude checks
Call to method isStrictVariables
from undeclared class \Twig\Environment
(Did you mean class \Pug\Twig\Environment) Open
'strict_variables' => $baseTwig->isStrictVariables(),
- Exclude checks
Parameter $twig
has undeclared type \Twig\Environment
(Did you mean class \Pug\Twig\Environment) Open
protected static function getPrivateExtensionProperty(TwigEnvironment $twig, string $extension, string $property)
- Exclude checks
Call to undeclared method \Pug\Twig\Environment::setExtensions
Open
$twig->setExtensions($extensions);
- Exclude checks
Call to undeclared method \Pug\Twig\Environment::getExtensions
Open
$this->extensions = $this->getExtensions();
- Exclude checks
Property \Pug\Twig\Environment->rootEnv
has undeclared type \Twig\Environment
(Did you mean class \Pug\Twig\Environment) Open
public $rootEnv;
- Exclude checks
Catching undeclared class \Twig\Error\RuntimeError
Open
} catch (RuntimeError $_) {
- Exclude checks
Parameter $container
has undeclared type \Symfony\Component\DependencyInjection\ContainerInterface
Open
public function setContainer(ContainerInterface $container): void
- Exclude checks
Parameter $source
has undeclared type \Twig\Source
(Did you mean resource) Open
public function compileSource(Source $source): string
- Exclude checks
Parameter $container
has undeclared type \Symfony\Component\DependencyInjection\ContainerInterface
Open
public static function fromTwigEnvironment(TwigEnvironment $baseTwig, PugSymfonyEngine $pugSymfonyEngine, ContainerInterface $container)
- Exclude checks
Call to method getCache
from undeclared class \Twig\Environment
(Did you mean class \Pug\Twig\Environment) Open
'cache' => $baseTwig->getCache(true),
- Exclude checks
Call to method getCharset
from undeclared class \Twig\Environment
(Did you mean class \Pug\Twig\Environment) Open
'charset' => $baseTwig->getCharset(),
- Exclude checks
Call to method isAutoReload
from undeclared class \Twig\Environment
(Did you mean class \Pug\Twig\Environment) Open
'auto_reload' => $baseTwig->isAutoReload(),
- Exclude checks
Call to method isDebug
from undeclared class \Twig\Environment
(Did you mean class \Pug\Twig\Environment) Open
'debug' => $baseTwig->isDebug(),
- Exclude checks
Call to undeclared method \Pug\Twig\Environment::tokenize
Open
$stream = $this->tokenize(new Source($code, $path, $path));
- Exclude checks
Call to undeclared method \Pug\Twig\Environment::addFilter
Open
$twig->addFilter($filter);
- Exclude checks
Call to method __construct
from undeclared class \Twig\Parser
Open
$parser = new Parser($this);
- Exclude checks
Call to method getLoader
from undeclared class \Twig\Environment
(Did you mean class \Pug\Twig\Environment) Open
$twig = new static($baseTwig->getLoader(), [
- Exclude checks
Line exceeds 120 characters; contains 122 characters Open
'optimizations' => static::getPrivateExtensionProperty($baseTwig, OptimizerExtension::class, 'optimizers'),
- Exclude checks
Line exceeds 120 characters; contains 140 characters Open
public static function fromTwigEnvironment(TwigEnvironment $baseTwig, PugSymfonyEngine $pugSymfonyEngine, ContainerInterface $container)
- Exclude checks
Line exceeds 120 characters; contains 125 characters Open
'autoescape' => static::getPrivateExtensionProperty($baseTwig, EscaperExtension::class, 'defaultStrategy'),
- Exclude checks
The variable $_ is not named in camelCase. Open
public function getRuntime(string $class)
{
try {
return parent::getRuntime($class);
} catch (RuntimeError $error) {
- Read upRead up
- Exclude checks
CamelCaseVariableName
Since: 0.2
It is considered best practice to use the camelCase notation to name variables.
Example
class ClassName {
public function doSomething() {
$data_module = new DataModule();
}
}