Function doTask
has a Cognitive Complexity of 17 (exceeds 5 allowed). Consider refactoring. Open
public function doTask($recordModel)
{
$mailerContent = [
'smtp_id' => ($this->smtp) ? $this->smtp : App\Mail::getDefaultSmtp(),
];
- 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 doTask
has 40 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function doTask($recordModel)
{
$mailerContent = [
'smtp_id' => ($this->smtp) ? $this->smtp : App\Mail::getDefaultSmtp(),
];
The method doTask() has an NPath complexity of 1280. The configured NPath complexity threshold is 200. Open
public function doTask($recordModel)
{
$mailerContent = [
'smtp_id' => ($this->smtp) ? $this->smtp : App\Mail::getDefaultSmtp(),
];
- Read upRead up
- Exclude checks
NPathComplexity
Since: 0.1
The NPath complexity of a method is the number of acyclic execution paths through that method. A threshold of 200 is generally considered the point where measures should be taken to reduce complexity.
Example
class Foo {
function bar() {
// lots of complicated code
}
}
Source https://phpmd.org/rules/codesize.html#npathcomplexity
The method doTask() has a Cyclomatic Complexity of 14. The configured cyclomatic complexity threshold is 10. Open
public function doTask($recordModel)
{
$mailerContent = [
'smtp_id' => ($this->smtp) ? $this->smtp : App\Mail::getDefaultSmtp(),
];
- 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 19 to the 15 allowed. Open
public function doTask($recordModel)
- 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 static access to class '\App\Mailer' in method 'doTask'. Open
\App\Mailer::addMail($mailerContent);
- 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 using static access to class '\App\EmailParser' in method 'doTask'. Open
$emailParser = \App\EmailParser::getInstanceByModel($recordModel);
- 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 using static access to class 'App\Mail' in method 'doTask'. Open
'smtp_id' => ($this->smtp) ? $this->smtp : App\Mail::getDefaultSmtp(),
- 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
The method doTask uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$mailerContent['from'] = ['email' => $key, 'name' => $value];
}
- Read upRead up
- Exclude checks
ElseExpression
Since: 1.4.0
An if expression with an else branch is basically not necessary. You can rewrite the conditions in a way that the else clause is not necessary and the code becomes simpler to read. To achieve this, use early return statements, though you may need to split the code it several smaller methods. For very simple assignments you could also use the ternary operations.
Example
class Foo
{
public function bar($flag)
{
if ($flag) {
// one branch
} else {
// another branch
}
}
}
Source https://phpmd.org/rules/cleancode.html#elseexpression
Avoid using static access to class '\App\TextParser' in method 'doTask'. Open
$textParser = \App\TextParser::getInstanceByModel($recordModel);
- 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
Define a constant instead of duplicating this literal "content" 3 times. Open
return ['subject', 'content', 'recepient', 'emailcc', 'emailbcc', 'fromEmail', 'smtp', 'emailoptout'];
- Read upRead up
- Exclude checks
Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.
On the other hand, constants can be referenced from many places, but only need to be updated in a single place.
Noncompliant Code Example
With the default threshold of 3:
function run() { prepare('action1'); // Non-Compliant - 'action1' is duplicated 3 times execute('action1'); release('action1'); }
Compliant Solution
ACTION_1 = 'action1'; function run() { prepare(ACTION_1); execute(ACTION_1); release(ACTION_1); }
Exceptions
To prevent generating some false-positives, literals having less than 5 characters are excluded.
Static call to undeclared method \App\EmailParser::getInstanceByModel
Open
$emailParser = \App\EmailParser::getInstanceByModel($recordModel);
- Exclude checks
Reference to undeclared property \VTEmailTask->emailbcc
Open
$bccEmail = $emailParser->setContent($this->emailbcc)->parse()->getContent(true);
- Exclude checks
Reference to undeclared property \VTEmailTask->fromEmail
Open
$fromEmailDetails = $emailParser->setContent($this->fromEmail)->parse()->getContent(true);
- Exclude checks
Reference to undeclared property \VTEmailTask->emailcc
Open
$ccEmail = $emailParser->setContent($this->emailcc)->parse()->getContent(true);
- Exclude checks
Reference to undeclared property \VTEmailTask->fromEmail
Open
if ($this->fromEmail) {
- Exclude checks
Reference to undeclared property \VTEmailTask->recepient
Open
$toEmail = $emailParser->setContent($this->recepient)->parse()->getContent(true);
- Exclude checks
Reference to undeclared property \VTEmailTask->content
(Did you mean expr->contents) Open
$mailerContent['content'] = $textParser->setContent($this->content)->parse()->getContent();
- Exclude checks
Reference to undeclared property \VTEmailTask->smtp
Open
'smtp_id' => ($this->smtp) ? $this->smtp : App\Mail::getDefaultSmtp(),
- Exclude checks
Reference to undeclared property \VTEmailTask->emailoptout
Open
$emailParser->emailoptout = $this->emailoptout ? true : false;
- Exclude checks
Reference to undeclared property \VTEmailTask->subject
Open
$mailerContent['subject'] = $textParser->setContent($this->subject)->parse()->getContent();
- Exclude checks
Call to method getInstanceByModel
from undeclared class \App\TextParser
(Did you mean class \Tests\App\TextParser) Open
$textParser = \App\TextParser::getInstanceByModel($recordModel);
- Exclude checks
Each class must be in a namespace of at least one level (a top-level vendor name) Open
class VTEmailTask extends VTTask
- Exclude checks
A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is defined on line 12 and the first side effect is on line 10. Open
<?php
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$mailerContent['to'] = $toEmail;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($fromEmailDetails) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
/**
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @param Vtiger_Record_Model $recordModel
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
\App\Mailer::addMail($mailerContent);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public $executeImmediately = true;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'smtp_id' => ($this->smtp) ? $this->smtp : App\Mail::getDefaultSmtp(),
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach ($fromEmailDetails as $key => $value) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
unset($emailParser);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (!empty($mailerContent['content'])) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
// Sending email takes more time, this should be handled via queue all the time.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
} else {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($toEmail) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$bccEmail = $emailParser->setContent($this->emailbcc)->parse()->getContent(true);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($this->fromEmail) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (\is_int($key)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$mailerContent['from'] = ['email' => $key, 'name' => $value];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$mailerContent['content'] = $textParser->setContent($this->content)->parse()->getContent();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
{
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
*
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$toEmail = $emailParser->setContent($this->recepient)->parse()->getContent(true);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$textParser = \App\TextParser::getInstanceByModel($recordModel);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$fromEmailDetails = $emailParser->setContent($this->fromEmail)->parse()->getContent(true);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$mailerContent['cc'] = $ccEmail;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public function doTask($recordModel)
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$emailParser->emailoptout = $this->emailoptout ? true : false;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (empty($toEmail) && empty($ccEmail) && empty($bccEmail)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
unset($textParser);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$mailerContent = [
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($bccEmail) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$mailerContent['from'] = ['email' => $value, 'name' => $value];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($ccEmail) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public function getFieldNames()
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
*/
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$emailParser = \App\EmailParser::getInstanceByModel($recordModel);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$mailerContent['subject'] = $textParser->setContent($this->subject)->parse()->getContent();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return ['subject', 'content', 'recepient', 'emailcc', 'emailbcc', 'fromEmail', 'smtp', 'emailoptout'];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
{
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$ccEmail = $emailParser->setContent($this->emailcc)->parse()->getContent(true);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$mailerContent['bcc'] = $bccEmail;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return false;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* Execute task.
- Exclude checks