Function getKey
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
public function getKey(string $key, $connection = null)
{
if (is_null($connection)) {
$connection = $this->getDefaultConnection();
}
- 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
Avoid using static access to class '\Poisa\Settings\Serializers\SerializerFactory' in method 'createKey'. Open
$serializable = SerializerFactory::createFromValue($value);
- 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 createKey uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$model->value = $serializable->serialize($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
The method updateKey uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$data->value = $serializable->serialize($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 '\Poisa\Settings\Serializers\SerializerFactory' in method 'getKey'. Open
$serializable = SerializerFactory::createFromTypeAlias($data->type_alias);
- 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 '\Poisa\Settings\Serializers\SerializerFactory' in method 'updateKey'. Open
$serializable = SerializerFactory::createFromValue($value);
- 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 getKey uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$value = $serializable->unserialize($data->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
Define a constant instead of duplicating this literal "settings.serializers" 3 times. Open
$serializers = config('settings.serializers');
- 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.
Possibly zero references to use statement for classlike/namespace Serializer
(\Poisa\Settings\Serializers\Serializer)
Open
use Poisa\Settings\Serializers\Serializer;
- Exclude checks
Call to undeclared function \config()
Open
return config('settings.system_connection');
- Exclude checks
Call to undeclared function \encrypt()
Open
$model->value = encrypt($serializable->serialize($value));
- Exclude checks
Call to undeclared function \decrypt()
Open
$value = $serializable->unserialize(decrypt($data->value));
- Exclude checks
Call to undeclared function \event()
Open
event(new SettingRead($key, $value, $connection));
- Exclude checks
Call to undeclared method \Poisa\Settings\Models\Settings::setConnection
Open
$model->setConnection(config("settings.{$connection}_connection"));
- Exclude checks
Call to undeclared method \Poisa\Settings\Models\Settings::where
Open
$data = $this->getConfiguredModel($connection)
- Exclude checks
Call to undeclared method \Poisa\Settings\Models\Settings::where
Open
$data = $model->where('key', $key)->first();
- Exclude checks
Call to undeclared function \config()
Open
$serializers = config('settings.serializers');
- Exclude checks
Call to undeclared function \encrypt()
Open
$data->value = encrypt($serializable->serialize($value));
- Exclude checks
Call to undeclared method \Poisa\Settings\Models\Settings::where
Open
$data = $model->where('key', $key)->first();
- Exclude checks
Call to undeclared function \config()
Open
return config('settings.serializers');
- Exclude checks
Call to undeclared method \Poisa\Settings\Models\Settings::save
Open
$success = $model->save();
- Exclude checks
Call to undeclared function \config()
Open
$data->setTable(config('settings.table_name'));
- Exclude checks
Call to undeclared function \event()
Open
event(new SettingUpdated($key, $value, $connection));
- Exclude checks
Call to undeclared function \config()
Open
config(['settings.serializers' => $serializers]);
- Exclude checks
Call to undeclared function \config()
Open
$model->setConnection(config("settings.{$connection}_connection"));
- Exclude checks
Call to undeclared method \Poisa\Settings\Models\Settings::setTable
Open
$model->setTable(config('settings.table_name'));
- Exclude checks
Call to undeclared function \config()
Open
if (config('settings.exception_if_key_not_found')) {
- Exclude checks
Call to undeclared function \event()
Open
event(new SettingCreated($key, $value, $connection));
- Exclude checks
Call to undeclared function \config()
Open
$model->setTable(config('settings.table_name'));
- Exclude checks