Function getActiveProviders
has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring. Open
public static function getActiveProviders(): array
{
if (self::$activeProvidersCache) {
return self::$activeProvidersCache;
}
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function getAllProviders
has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring. Open
public static function getAllProviders(): array
{
if (self::$providersCache) {
return self::$providersCache;
}
- 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 '83', column '17'). Open
foreach ((new \DirectoryIterator(ROOT_DIRECTORY . \DIRECTORY_SEPARATOR . 'app/Map/Address')) as $fileinfo) {
- 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 '120', column '17'). Open
$query = (new \App\Db\Query())->from('s_#__address_finder_config');
- 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 assigning values to variables in if clauses and the like (line '84', column '59'). Open
public static function getAllProviders(): array
{
if (self::$providersCache) {
return self::$providersCache;
}
- 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\Cache' in method 'getConfig'. Open
if (\App\Cache::has('AddressFinder', 'Config')) {
- 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 getActiveProviders uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
foreach (self::getAllProviders() as $provider) {
if ($provider->isActive()) {
self::$activeProvidersCache[] = $provider->getName();
}
- 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\YetiForce\Shop' in method 'getAllProviders'. Open
if($fileName === 'YetiForceGeocoder' && !\App\YetiForce\Shop::check('YetiForceGeocoder')){
- 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\Cache' in method 'getConfig'. Open
\App\Cache::save('AddressFinder', 'Config', $config, \App\Cache::LONG);
- 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\Cache' in method 'getConfig'. Open
return \App\Cache::get('AddressFinder', 'Config');
- 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 "Config" 3 times. Open
if (\App\Cache::has('AddressFinder', 'Config')) {
- 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 "AddressFinder" 3 times. Open
if (\App\Cache::has('AddressFinder', 'Config')) {
- 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 method getName
from undeclared class \App\Map\Address\Base
(Did you mean class \App\Base or class \App\Cache\Base or class \App\Cli\Base or class \App\Controller\Base or class \App\Controller\View\Base or class \App\Db\Importers\Base or class \Tests\Base) Open
self::$activeProvidersCache[] = $provider->getName();
- Exclude checks
Call to method isActive
from undeclared class \App\Map\Address\Base
(Did you mean class \App\Base or class \App\Cache\Base or class \App\Cli\Base or class \App\Controller\Base or class \App\Controller\View\Base or class \App\Db\Importers\Base or class \Tests\Base) Open
if ($provider->isActive()) {
- Exclude checks
Return type of getAllProviders()
is undeclared type \App\Map\Address\Base[]
(Did you mean class \App\Base or class \App\Cache\Base or class \App\Cli\Base or class \App\Controller\Base or class \App\Controller\View\Base or class \App\Db\Importers\Base or class \Tests\Base) Open
public static function getAllProviders(): array
- Exclude checks
Call to undeclared method \App\Db\Query::from
Open
$query = (new \App\Db\Query())->from('s_#__address_finder_config');
- Exclude checks
Call to method check
from undeclared class \App\YetiForce\Shop
Open
if($fileName === 'YetiForceGeocoder' && !\App\YetiForce\Shop::check('YetiForceGeocoder')){
- Exclude checks
Return type of getInstance()
is undeclared type \App\Map\Address\Base
(Did you mean class \App\Base or class \App\Cache\Base or class \App\Cli\Base or class \App\Controller\Base or class \App\Controller\View\Base or class \App\Db\Importers\Base or class \Tests\Base) Open
public static function getInstance($type): Address\Base
- Exclude checks
Property \App\Map\Address::$providersCache
has undeclared type \App\Map\Address\Base[]
(Did you mean class \App\Base or class \App\Cache\Base or class \App\Cli\Base or class \App\Controller\Base or class \App\Controller\View\Base or class \App\Db\Importers\Base or class \Tests\Base) Open
private static $providersCache = [];
- Exclude checks
Property \App\Map\Address::$providerInstanceCache
has undeclared type \App\Map\Address\Base[]
(Did you mean class \App\Base or class \App\Cache\Base or class \App\Cli\Base or class \App\Controller\Base or class \App\Controller\View\Base or class \App\Db\Importers\Base or class \Tests\Base) Open
private static $providerInstanceCache = [];
- Exclude checks
Call to method getName
from undeclared class \App\Map\Address\Base
(Did you mean class \App\Base or class \App\Cache\Base or class \App\Cli\Base or class \App\Controller\Base or class \App\Controller\View\Base or class \App\Db\Importers\Base or class \Tests\Base) Open
self::$activeProvidersCache[] = $provider->getName();
- Exclude checks
Call to method isActive
from undeclared class \App\Map\Address\Base
(Did you mean class \App\Base or class \App\Cache\Base or class \App\Cli\Base or class \App\Controller\Base or class \App\Controller\View\Base or class \App\Db\Importers\Base or class \Tests\Base) Open
if ($provider->isActive()) {
- Exclude checks
Avoid excessively long variable names like $providerInstanceCache. Keep variable name length under 20. Open
private static $providerInstanceCache = [];
- Read upRead up
- Exclude checks
LongVariable
Since: 0.2
Detects when a field, formal or local variable is declared with a long name.
Example
class Something {
protected $reallyLongIntName = -3; // VIOLATION - Field
public static function main( array $interestingArgumentsList[] ) { // VIOLATION - Formal
$otherReallyLongName = -5; // VIOLATION - Local
for ($interestingIntIndex = 0; // VIOLATION - For
$interestingIntIndex < 10;
$interestingIntIndex++ ) {
}
}
}
Source https://phpmd.org/rules/naming.html#longvariable
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
while ($row = $dataReader->read()) {
- 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 ('php' === $fileinfo->getExtension() && 'Base' !== ($fileName = $fileinfo->getBasename('.php'))) {
- 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 \App\Cache::get('AddressFinder', 'Config');
- 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($fileName === 'YetiForceGeocoder' && !\App\YetiForce\Shop::check('YetiForceGeocoder')){
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (isset(self::$providerInstanceCache[$type])) {
- 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 (self::$providersCache) {
- 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
$dataReader = $query->createCommand()->query();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (!$defaultProvider) {
- 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
public static function getActiveProviders(): array
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
self::$providersCache[$fileName] = static::getInstance($fileName);
- 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 self::$providerInstanceCache[$type];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* Get config for address finder.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (\App\Cache::has('AddressFinder', 'Config')) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
private static $providerInstanceCache = [];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @return string
- 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
self::$activeProvidersCache[] = $provider->getName();
- 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
foreach ((new \DirectoryIterator(ROOT_DIRECTORY . \DIRECTORY_SEPARATOR . 'app/Map/Address')) as $fileinfo) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public static function getDefaultProvider(): string
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @return string[]
- 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 ($provider->isActive()) {
- 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 string $type
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @return \App\Map\Address\Base
- 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
public static function getConfig(): array
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$query = (new \App\Db\Query())->from('s_#__address_finder_config');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$config = [];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
private static $providersCache = [];
- 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 (self::$activeProvidersCache) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return self::$activeProvidersCache;
- 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
* Get all providers for address finder.
- 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
foreach (self::getAllProviders() as $provider) {
- 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
$className = "\\App\\Map\\Address\\$type";
- 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 \App\Map\Address\Base[] Providers cache. */
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
/** @var string[] Active providers cache. */
- 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 self::$providersCache;
- 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
$defaultProvider = static::getConfig()['global']['default_provider'] ?? '';
- 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
/** @var Address\Base[] Providers instance cache. */
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* Get default provider.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$provider = static::getActiveProviders();
- 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 $defaultProvider;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (self::$providersCache) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach (self::$providersCache as $provider) {
- 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
* Get address finder instance by type.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($provider) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* Get active providers for address finder.
- 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 ($provider->isActive()) {
- 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 array
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$config[$row['type']][$row['name']] = $row['val'];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return $config;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
private static $activeProvidersCache = [];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$defaultProvider = \current($provider);
- 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 self::$activeProvidersCache;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @return \App\Map\Address\Base[]
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public static function getAllProviders(): array
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return self::$providersCache;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return self::$providerInstanceCache[$type] = new $className();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
\App\Cache::save('AddressFinder', 'Config', $config, \App\Cache::LONG);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
self::$activeProvidersCache[] = $provider->getName();
- 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
continue;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public static function getInstance($type): Address\Base
- Exclude checks
Expected 1 space after IF keyword; 0 found Open
if($fileName === 'YetiForceGeocoder' && !\App\YetiForce\Shop::check('YetiForceGeocoder')){
- Exclude checks
Expected 1 space after closing parenthesis; found 0 Open
if($fileName === 'YetiForceGeocoder' && !\App\YetiForce\Shop::check('YetiForceGeocoder')){
- Exclude checks