Function doTask
has a Cognitive Complexity of 22 (exceeds 5 allowed). Consider refactoring. Open
public function doTask($recordModel)
{
if (!\App\Module::isModuleActive('Calendar')) {
return;
}
- 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 72 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function doTask($recordModel)
{
if (!\App\Module::isModuleActive('Calendar')) {
return;
}
The method doTask() has an NPath complexity of 784. The configured NPath complexity threshold is 200. Open
public function doTask($recordModel)
{
if (!\App\Module::isModuleActive('Calendar')) {
return;
}
- 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 15. The configured cyclomatic complexity threshold is 10. Open
public function doTask($recordModel)
{
if (!\App\Module::isModuleActive('Calendar')) {
return;
}
- 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 22 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
The class VTCreateEventTask has a coupling between objects value of 13. Consider to reduce the number of dependencies under 13. Open
class VTCreateEventTask extends VTTask
{
public $executeImmediately = true;
public function getFieldNames()
- Read upRead up
- Exclude checks
CouplingBetweenObjects
Since: 1.1.0
A class with too many dependencies has negative impacts on several quality aspects of a class. This includes quality criteria like stability, maintainability and understandability
Example
class Foo {
/**
* @var \foo\bar\X
*/
private $x = null;
/**
* @var \foo\bar\Y
*/
private $y = null;
/**
* @var \foo\bar\Z
*/
private $z = null;
public function setFoo(\Foo $foo) {}
public function setBar(\Bar $bar) {}
public function setBaz(\Baz $baz) {}
/**
* @return \SplObjectStorage
* @throws \OutOfRangeException
* @throws \InvalidArgumentException
* @throws \ErrorException
*/
public function process(\Iterator $it) {}
// ...
}
Source https://phpmd.org/rules/design.html#couplingbetweenobjects
Missing class import via use statement (line '51', column '23'). Open
$userExists = (new App\Db\Query())->from('vtiger_users')
- 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
Missing class import via use statement (line '57', column '24'). Open
$groupExist = (new App\Db\Query())->from('vtiger_groups')
- 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
Missing class import via use statement (line '132', column '15'). Open
$date = new DateTime();
- 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
Avoid using static access to class '\App\User' in method 'doTask'. Open
$userId = \App\User::getCurrentUserId();
- 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 assigning values to variables in if clauses and the like (line '90', column '8'). Open
public function doTask($recordModel)
{
if (!\App\Module::isModuleActive('Calendar')) {
return;
}
- Read upRead up
- Exclude checks
IfStatementAssignment
Since: 2.7.0
Assignments in if clauses and the like are considered a code smell. Assignments in PHP return the right operand as their result. In many cases, this is an expected behavior, but can lead to many difficult to spot bugs, especially when the right operand could result in zero, null or an empty string and the like.
Example
class Foo
{
public function bar($flag)
{
if ($foo = 'bar') { // possible typo
// ...
}
if ($baz = 0) { // always false
// ...
}
}
}
Source http://phpmd.org/rules/cleancode.html#ifstatementassignment
Avoid assigning values to variables in if clauses and the like (line '84', column '7'). Open
public function doTask($recordModel)
{
if (!\App\Module::isModuleActive('Calendar')) {
return;
}
- Read upRead up
- Exclude checks
IfStatementAssignment
Since: 2.7.0
Assignments in if clauses and the like are considered a code smell. Assignments in PHP return the right operand as their result. In many cases, this is an expected behavior, but can lead to many difficult to spot bugs, especially when the right operand could result in zero, null or an empty string and the like.
Example
class Foo
{
public function bar($flag)
{
if ($foo = 'bar') { // possible typo
// ...
}
if ($baz = 0) { // always false
// ...
}
}
}
Source http://phpmd.org/rules/cleancode.html#ifstatementassignment
Avoid using static access to class '\App\Module' in method 'doTask'. Open
if (!\App\Module::isModuleActive('Calendar')) {
- 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 'Users' in method 'doTask'. Open
$userId = Users::getActiveAdminUser();
- 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\Record' in method 'doTask'. Open
$parentModuleName = \App\Record::getType($parentRecord);
- 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\ModuleHierarchy' in method 'doTask'. Open
$field = \App\ModuleHierarchy::getMappingRelatedField($moduleName);
- 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\Record' in method 'doTask'. Open
if ($parentRecord = \App\Record::getParentRecord($id)) {
- 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 'Vtiger_Record_Model' in method 'doTask'. Open
$newRecordModel = Vtiger_Record_Model::getCleanInstance('Calendar');
- 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 '\Vtiger_Relation_Model' in method 'doTask'. Open
$relationModel = \Vtiger_Relation_Model::getInstance($recordModel->getModule(), $newRecordModel->getModule());
- 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\ModuleHierarchy' in method 'doTask'. Open
$field = \App\ModuleHierarchy::getMappingRelatedField($parentModuleName);
- 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 'DateTimeField' in method 'convertToDBFormat'. Open
$dbInsertDateTime = DateTimeField::convertToDBTimeZone($date->format('Y-m-d') . ' ' . $time);
- 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\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
Avoid using static access to class '\App\Record' in method 'doTask'. Open
$parentModuleName = \App\Record::getType($parentRecord);
- 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 {
$groupExist = (new App\Db\Query())->from('vtiger_groups')
->where(['groupid' => $this->assigned_user_id])
->exists();
if ($groupExist) {
- 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\Fields\Time' in method 'convertToDBFormat'. Open
$time = \App\Fields\Time::sanitizeDbFormat($timeStr);
- 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\Record' in method 'doTask'. Open
if ($parentRecord = \App\Record::getParentRecord($parentRecord)) {
- 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 conv12to24hour uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$hours = ((int) $arr[1]) % 12 + 12;
}
- 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\ModuleHierarchy' in method 'doTask'. Open
$field = \App\ModuleHierarchy::getMappingRelatedField($parentModuleName);
- 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 "assigned_user_id" 4 times. Open
'status', 'priority', 'assigned_user_id', ];
- 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.
Reference to undeclared property \VTCreateEventTask->startDays
Open
$startDate = $this->calculateDate($recordModel, $this->startDays, $this->startDirection, $this->startDatefield);
- Exclude checks
Reference to undeclared property \VTCreateEventTask->endDirection
Open
$endDate = $this->calculateDate($recordModel, $this->endDays, $this->endDirection, $this->endDatefield);
- Exclude checks
Reference to undeclared property \VTCreateEventTask->assigned_user_id
Open
$userId = $this->assigned_user_id;
- Exclude checks
Reference to undeclared property \VTCreateEventTask->description
Open
'description' => $textParser->setContent($this->description)->parse()->getContent(),
- Exclude checks
Reference to undeclared property \VTCreateEventTask->startDirection
Open
$startDate = $this->calculateDate($recordModel, $this->startDays, $this->startDirection, $this->startDatefield);
- Exclude checks
Reference to undeclared property \VTCreateEventTask->endTime
Open
'time_end' => self::convertToDBFormat($this->endTime),
- Exclude checks
Reference to undeclared property \VTCreateEventTask->startDatefield
Open
$startDate = $this->calculateDate($recordModel, $this->startDays, $this->startDirection, $this->startDatefield);
- Exclude checks
Reference to undeclared property \VTCreateEventTask->assigned_user_id
Open
} elseif (!empty($this->assigned_user_id)) { // Added to check if the user/group is active
- Exclude checks
Reference to undeclared property \VTCreateEventTask->assigned_user_id
Open
->where(['groupid' => $this->assigned_user_id])
- Exclude checks
Reference to undeclared property \VTCreateEventTask->eventType
Open
'activitytype' => $this->eventType,
- Exclude checks
Reference to undeclared property \VTCreateEventTask->assigned_user_id
Open
} elseif ('triggerUser' === $this->assigned_user_id) {
- Exclude checks
Call to undeclared method \App\Db\Query::from
Open
$userExists = (new App\Db\Query())->from('vtiger_users')
- Exclude checks
Reference to undeclared property \VTCreateEventTask->eventName
Open
'subject' => $textParser->setContent($this->eventName)->parse()->getContent(),
- Exclude checks
Reference to undeclared property \VTCreateEventTask->priority
Open
'taskpriority' => $this->priority,
- Exclude checks
Return type of convertToDBFormat()
is undeclared type \time
Open
public static function convertToDBFormat($timeStr)
- Exclude checks
Argument 1 (input)
is int
but \str_pad()
takes string
Open
return str_pad($hours, 2, '0', STR_PAD_LEFT) . ':' . str_pad($arr[2], 2, '0', STR_PAD_LEFT);
- Exclude checks
Call to undeclared method \App\Db\Query::from
Open
$groupExist = (new App\Db\Query())->from('vtiger_groups')
- Exclude checks
Reference to undeclared property \VTCreateEventTask->startTime
Open
'time_start' => self::convertToDBFormat($this->startTime),
- Exclude checks
Reference to undeclared property \VTCreateEventTask->endDatefield
Open
$endDate = $this->calculateDate($recordModel, $this->endDays, $this->endDirection, $this->endDatefield);
- Exclude checks
Call to method getCurrentUserId
from undeclared class \App\User
(Did you mean class \Tests\App\User) Open
$userId = \App\User::getCurrentUserId();
- Exclude checks
Reference to undeclared property \Vtiger_Record_Model->executeUser
Open
$userId = $recordModel->executeUser;
- Exclude checks
Reference to undeclared property \VTCreateEventTask->assigned_user_id
Open
$userId = $this->assigned_user_id;
- Exclude checks
Reference to undeclared property \VTCreateEventTask->assigned_user_id
Open
->where(['id' => $this->assigned_user_id, 'status' => 'Active'])
- 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
Reference to undeclared property \VTCreateEventTask->endDays
Open
$endDate = $this->calculateDate($recordModel, $this->endDays, $this->endDirection, $this->endDatefield);
- Exclude checks
Returning type string
but convertToDBFormat()
is declared to return \time
Open
return $dbInsertDateTime->format('H:i:s');
- Exclude checks
Reference to undeclared property \VTCreateEventTask->assigned_user_id
Open
} elseif ('copyParentOwner' === $this->assigned_user_id) {
- Exclude checks
Reference to undeclared property \VTCreateEventTask->status
Open
'activitystatus' => $this->status,
- Exclude checks
Reference to undeclared property \VTCreateEventTask->assigned_user_id
Open
if ('currentUser' === $this->assigned_user_id) {
- Exclude checks
Identical blocks of code found in 2 locations. Consider refactoring. Open
if ('currentUser' === $this->assigned_user_id) {
$userId = \App\User::getCurrentUserId();
} elseif ('triggerUser' === $this->assigned_user_id) {
$userId = $recordModel->executeUser;
} elseif ('copyParentOwner' === $this->assigned_user_id) {
- Read upRead up
Duplicated Code
Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).
Tuning
This issue has a mass of 178.
We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.
The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.
If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.
See codeclimate-duplication
's documentation for more information about tuning the mass threshold in your .codeclimate.yml
.
Refactorings
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76
Identical blocks of code found in 2 locations. Consider refactoring. Open
public static function conv12to24hour($timeStr)
{
$arr = [];
preg_match('/(\d{1,2}):(\d{1,2})(am|pm)/', $timeStr, $arr);
if ('am' == $arr[3]) {
- Read upRead up
Duplicated Code
Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).
Tuning
This issue has a mass of 139.
We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.
The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.
If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.
See codeclimate-duplication
's documentation for more information about tuning the mass threshold in your .codeclimate.yml
.
Refactorings
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76
Identical blocks of code found in 2 locations. Consider refactoring. Open
if ($parentRecord = \App\Record::getParentRecord($id)) {
$parentModuleName = \App\Record::getType($parentRecord);
$field = \App\ModuleHierarchy::getMappingRelatedField($parentModuleName);
if ($field) {
$fields[$field] = $parentRecord;
- Read upRead up
Duplicated Code
Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).
Tuning
This issue has a mass of 105.
We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.
The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.
If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.
See codeclimate-duplication
's documentation for more information about tuning the mass threshold in your .codeclimate.yml
.
Refactorings
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76
Each class must be in a namespace of at least one level (a top-level vendor name) Open
class VTCreateEventTask 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 14 and the first side effect is on line 10. Open
<?php
- Exclude checks
Avoid variables with short names like $id. Configured minimum length is 3. Open
$id = $recordModel->getId();
- 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
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
'date_start' => $startDate,
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$newRecordModel = Vtiger_Record_Model::getCleanInstance('Calendar');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$newRecordModel->setHandlerExceptions(['disableHandlerClasses' => ['Vtiger_Workflow_Handler']]);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$fields = [
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'taskpriority' => $this->priority,
- 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 ($parentRecord = \App\Record::getParentRecord($id)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* To convert time_start & time_end values to db format.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @param string $timeStr
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (!\App\Module::isModuleActive('Calendar')) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$userId = Users::getActiveAdminUser();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$endDate = $this->calculateDate($recordModel, $this->endDays, $this->endDirection, $this->endDatefield);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($field) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$baseDate = strtotime($match[0]);
- 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
* Execute task.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
->where(['groupid' => $this->assigned_user_id])
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'subject' => $textParser->setContent($this->eventName)->parse()->getContent(),
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($field) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$relationModel = \Vtiger_Relation_Model::getInstance($recordModel->getModule(), $newRecordModel->getModule());
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$relationModel->addRelation($recordModel->getId(), $newRecordModel->getId());
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$arr = [];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return ['startTime', 'endTime'];
- 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
$moduleName = $recordModel->getModuleName();
- 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
$id = $recordModel->getId();
- 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
preg_match('/\d\d\d\d-\d\d-\d\d/', $baseDate, $match);
- 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 ($userExists) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'activitystatus' => $this->status,
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($parentRecord = \App\Record::getParentRecord($parentRecord)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$newRecordModel->save();
- 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
$userId = $this->assigned_user_id;
- 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
* @return time
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$date = new DateTime();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
preg_match('/(\d{1,2}):(\d{1,2})(am|pm)/', $timeStr, $arr);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return ['eventType', 'eventName', 'description', 'sendNotification',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
->exists();
- 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
$field = \App\ModuleHierarchy::getMappingRelatedField($moduleName);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$parentModuleName = \App\Record::getType($parentRecord);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$days = 0;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public static function convertToDBFormat($timeStr)
- 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
$userId = $recordModel->get('assigned_user_id');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$startDate = $this->calculateDate($recordModel, $this->startDays, $this->startDirection, $this->startDatefield);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'time_start' => self::convertToDBFormat($this->startTime),
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'time_end' => self::convertToDBFormat($this->endTime),
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'due_date' => $endDate,
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$fields[$field] = $id;
- 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
$baseDate = date('Y-m-d');
- 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
return strftime('%Y-%m-%d', $baseDate + $days * 24 * 60 * 60 *
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$dbInsertDateTime = DateTimeField::convertToDBTimeZone($date->format('Y-m-d') . ' ' . $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
$hours = ((int) $arr[1]) % 12 + 12;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public function getTimeFieldList()
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (null === $userId) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
} elseif ('triggerUser' === $this->assigned_user_id) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$userId = $recordModel->get('assigned_user_id');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$parentModuleName = \App\Record::getType($parentRecord);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ('' == $days) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
('before' == strtolower($direction) ? -1 : 1));
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public static function conv12to24hour($timeStr)
- 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
$userExists = (new App\Db\Query())->from('vtiger_users')
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
private function calculateDate($recordModel, $days, $direction, $datefield)
- 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
if ('am' == $arr[3]) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$userId = \App\User::getCurrentUserId();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
->where(['id' => $this->assigned_user_id, 'status' => 'Active'])
- 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
if ($groupExist) {
- 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
'activitytype' => $this->eventType,
- 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
return $dbInsertDateTime->format('H:i:s');
- 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
'status', 'priority', 'assigned_user_id', ];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
} elseif ('copyParentOwner' === $this->assigned_user_id) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$userId = $this->assigned_user_id;
- 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
$newRecordModel->setData($fields);
- 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
'endTime', 'endDays', 'endDirection', 'endDatefield',
- 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
$groupExist = (new App\Db\Query())->from('vtiger_groups')
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$baseDate = $recordModel->get($datefield);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ('' == $baseDate) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$time = \App\Fields\Time::sanitizeDbFormat($timeStr);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return str_pad($hours, 2, '0', STR_PAD_LEFT) . ':' . str_pad($arr[2], 2, '0', STR_PAD_LEFT);
- 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
$userId = $recordModel->executeUser;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
} elseif (!empty($this->assigned_user_id)) { // Added to check if the user/group is active
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
->exists();
- 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
'description' => $textParser->setContent($this->description)->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
if ($relationModel) {
- 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
'startTime', 'startDays', 'startDirection', 'startDatefield',
- 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
{
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ('currentUser' === $this->assigned_user_id) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'assigned_user_id' => $userId,
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'duration_hours' => 0,
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$field = \App\ModuleHierarchy::getMappingRelatedField($parentModuleName);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$fields[$field] = $parentRecord;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$field = \App\ModuleHierarchy::getMappingRelatedField($parentModuleName);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($field) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$fields[$field] = $parentRecord;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$hours = ((int) $arr[1]) % 12;
- Exclude checks