Function transfer
has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring. Open
public function transfer(int $relatedRecordId, int $fromRecordId, int $toRecordId): bool
{
$result = $this->updateDB($toRecordId, ['crmid' => $fromRecordId, 'ossmailviewid' => $relatedRecordId]);
if ($result && $parentId = \Users_Privileges_Model::getParentRecord($toRecordId)) {
$parentIdFromRecordId = \Users_Privileges_Model::getParentRecord($fromRecordId);
- 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
Missing class import via use statement (line '84', column '22'). Open
return (bool) (new \App\Db\Query())->from(self::TABLE_NAME)->where($data)->exists();
- 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 '\Users_Privileges_Model' in method 'transfer'. Open
if ($result && $parentId = \Users_Privileges_Model::getParentRecord($toRecordId)) {
- 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 '53', column '18'). Open
public function transfer(int $relatedRecordId, int $fromRecordId, int $toRecordId): bool
{
$result = $this->updateDB($toRecordId, ['crmid' => $fromRecordId, 'ossmailviewid' => $relatedRecordId]);
if ($result && $parentId = \Users_Privileges_Model::getParentRecord($toRecordId)) {
$parentIdFromRecordId = \Users_Privileges_Model::getParentRecord($fromRecordId);
- 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 '\Users_Privileges_Model' in method 'transfer'. Open
if ($parentIdFromRecordId && ($parentIdFromRecordId = \Users_Privileges_Model::getParentRecord($parentIdFromRecordId)) && $this->isExists(['crmid' => $parentIdFromRecordId] + $data)) {
- 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 transfer uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$date = \Vtiger_Record_Model::getInstanceById($relatedRecordId, 'OSSMailView')->get('date');
$this->addToDB(['crmid' => $parentId, 'date' => $date] + $data);
}
- 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 assigning values to variables in if clauses and the like (line '64', column '35'). Open
public function transfer(int $relatedRecordId, int $fromRecordId, int $toRecordId): bool
{
$result = $this->updateDB($toRecordId, ['crmid' => $fromRecordId, 'ossmailviewid' => $relatedRecordId]);
if ($result && $parentId = \Users_Privileges_Model::getParentRecord($toRecordId)) {
$parentIdFromRecordId = \Users_Privileges_Model::getParentRecord($fromRecordId);
- 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 '\Users_Privileges_Model' in method 'transfer'. Open
$parentIdFromRecordId = \Users_Privileges_Model::getParentRecord($fromRecordId);
- 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_Privileges_Model' in method 'transfer'. Open
$parentId = \Users_Privileges_Model::getParentRecord($parentId);
- 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 transfer uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$date = \Vtiger_Record_Model::getInstanceById($relatedRecordId, 'OSSMailView')->get('date');
$this->addToDB(['crmid' => $parentId, 'date' => $date] + $data);
}
- 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
Define a constant instead of duplicating this literal "crmid" 11 times. Open
return (bool) \App\Db::getInstance()->createCommand()->delete(self::TABLE_NAME, ['crmid' => $sourceRecordId, 'ossmailviewid' => $destinationRecordId])->execute();
- 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.
Define a constant instead of duplicating this literal "ossmailviewid" 7 times. Open
return (bool) \App\Db::getInstance()->createCommand()->delete(self::TABLE_NAME, ['crmid' => $sourceRecordId, 'ossmailviewid' => $destinationRecordId])->execute();
- 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.
Call to undeclared method \App\Db::createCommand
Open
$result = (bool) \App\Db::getInstance()->createCommand()->update(self::TABLE_NAME, ['crmid' => $toRecordId], $where)->execute();
- Exclude checks
Call to undeclared method \App\Db::createCommand
Open
$result = (bool) \App\Db::getInstance()->createCommand()->insert(self::TABLE_NAME, $data)->execute();
- Exclude checks
Call to method addJoin
from undeclared class \App\QueryGenerator
(Did you mean class \Tests\App\QueryGenerator) Open
$this->relationModel->getQueryGenerator()
- Exclude checks
Call to undeclared method \App\Db::createCommand
Open
return (bool) \App\Db::getInstance()->createCommand()->delete(self::TABLE_NAME, ['crmid' => $sourceRecordId, 'ossmailviewid' => $destinationRecordId])->execute();
- Exclude checks
Call to undeclared method \App\Db\Query::from
Open
return (bool) (new \App\Db\Query())->from(self::TABLE_NAME)->where($data)->exists();
- Exclude checks
Each class must be in a namespace of at least one level (a top-level vendor name) Open
class OSSMailView_GetEmails_Relation extends \App\Relation\RelationAbstraction
- Exclude checks
The class OSSMailView_GetEmails_Relation is not named in CamelCase. Open
class OSSMailView_GetEmails_Relation extends \App\Relation\RelationAbstraction
{
/**
* @var string Name of the table that stores relations.
*/
- Read upRead up
- Exclude checks
CamelCaseClassName
Since: 0.2
It is considered best practice to use the CamelCase notation to name classes.
Example
class class_name {
}
Source
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
* Check if relation exists.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @param array $data
- 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
public const TABLE_NAME = 'vtiger_ossmailview_relation';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($parentIdFromRecordId && $this->isExists($data)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$date = \Vtiger_Record_Model::getInstanceById($relatedRecordId, 'OSSMailView')->get('date');
- 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
* @var string Name of the table that stores relations.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return Vtiger_Relation_Model::RELATION_M2M;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
->addNativeCondition([self::TABLE_NAME . '.crmid' => $this->relationModel->get('parentRecord')->getId()]);
- Exclude checks
Line exceeds 120 characters; contains 170 characters Open
return (bool) \App\Db::getInstance()->createCommand()->delete(self::TABLE_NAME, ['crmid' => $sourceRecordId, 'ossmailviewid' => $destinationRecordId])->execute();
- 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 bool
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return (bool) (new \App\Db\Query())->from(self::TABLE_NAME)->where($data)->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
public function create(int $sourceRecordId, int $destinationRecordId): bool
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$parentIdFromRecordId = \Users_Privileges_Model::getParentRecord($fromRecordId);
- 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 (bool) \App\Db::getInstance()->createCommand()->delete(self::TABLE_NAME, ['crmid' => $sourceRecordId, 'ossmailviewid' => $destinationRecordId])->execute();
- 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
/** {@inheritdoc} */
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($parentIdFromRecordId && ($parentIdFromRecordId = \Users_Privileges_Model::getParentRecord($parentIdFromRecordId)) && $this->isExists(['crmid' => $parentIdFromRecordId] + $data)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$this->addToDB(['crmid' => $parentId, 'date' => $date] + $data);
- 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
* Add relation to DB.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public function addToDB(array $data): bool
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
/** {@inheritdoc} */
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public function getQuery()
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$this->relationModel->getQueryGenerator()
- 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
$this->updateDB($parentId, ['crmid' => $parentIdFromRecordId] + $data);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$date = \Vtiger_Record_Model::getInstanceById($relatedRecordId, 'OSSMailView')->get('date');
- Exclude checks
Line exceeds 120 characters; contains 200 characters Open
if ($parentIdFromRecordId && ($parentIdFromRecordId = \Users_Privileges_Model::getParentRecord($parentIdFromRecordId)) && $this->isExists(['crmid' => $parentIdFromRecordId] + $data)) {
- 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
$parentId = \Users_Privileges_Model::getParentRecord($parentId);
- 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
public function isExists(array $data): bool
- 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
public function delete(int $sourceRecordId, int $destinationRecordId): bool
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
/** {@inheritdoc} */
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$data = ['ossmailviewid' => $relatedRecordId, 'crmid' => $parentIdFromRecordId];
- 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
/** {@inheritdoc} */
- 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
$this->addToDB(['crmid' => $parentId, 'date' => $date] + $data);
- 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
return 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
->addJoin(['INNER JOIN', self::TABLE_NAME, self::TABLE_NAME . '.ossmailviewid = vtiger_ossmailview.ossmailviewid'])
- Exclude checks
Line exceeds 120 characters; contains 127 characters Open
->addJoin(['INNER JOIN', self::TABLE_NAME, self::TABLE_NAME . '.ossmailviewid = vtiger_ossmailview.ossmailviewid'])
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return $result;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public function transfer(int $relatedRecordId, int $fromRecordId, int $toRecordId): bool
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$this->updateDB($parentId, $data);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($parentId) {
- 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 bool
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public function getRelationType(): int
- 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
/** {@inheritdoc} */
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* Update relation.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @return bool
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$result = (bool) \App\Db::getInstance()->createCommand()->update(self::TABLE_NAME, ['crmid' => $toRecordId], $where)->execute();
- 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 array $where
- Exclude checks
Line exceeds 120 characters; contains 140 characters Open
$result = (bool) \App\Db::getInstance()->createCommand()->update(self::TABLE_NAME, ['crmid' => $toRecordId], $where)->execute();
- 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 $result;
- 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
return $result;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public function updateDB(int $toRecordId, array $where): bool
- 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
$result = $this->updateDB($toRecordId, ['crmid' => $fromRecordId, 'ossmailviewid' => $relatedRecordId]);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($result && $parentId = \Users_Privileges_Model::getParentRecord($toRecordId)) {
- 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
* @param array $data
- 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
$result = (bool) \App\Db::getInstance()->createCommand()->insert(self::TABLE_NAME, $data)->execute();
- 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 int $toRecordId
- 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
$result = true;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (!$this->isExists(['ossmailviewid' => $data['ossmailviewid'], 'crmid' => $data['crmid']])) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$result = true;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (!$this->isExists(['ossmailviewid' => $where['ossmailviewid'], 'crmid' => $toRecordId])) {
- Exclude checks
Class name "OSSMailView_GetEmails_Relation" is not in camel caps format Open
class OSSMailView_GetEmails_Relation extends \App\Relation\RelationAbstraction
- Exclude checks