Function read
has a Cognitive Complexity of 18 (exceeds 5 allowed). Consider refactoring. Open
public function read()
{
$defaultCharset = App\Config::main('default_charset');
$filePath = $this->getFilePath();
- 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 read
has 39 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function read()
{
$defaultCharset = App\Config::main('default_charset');
$filePath = $this->getFilePath();
Refactor this function to reduce its Cognitive Complexity from 17 to the 15 allowed. Open
public function read()
- 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 method getFirstRowData has a boolean flag argument $hasHeader, which is a certain sign of a Single Responsibility Principle violation. Open
public function getFirstRowData($hasHeader = true)
- Read upRead up
- Exclude checks
BooleanArgumentFlag
Since: 1.4.0
A boolean flag argument is a reliable indicator for a violation of the Single Responsibility Principle (SRP). You can fix this problem by extracting the logic in the boolean flag into its own class or method.
Example
class Foo {
public function bar($flag = true) {
}
}
Source https://phpmd.org/rules/cleancode.html#booleanargumentflag
Avoid using static access to class 'App\Config' in method 'getFirstRowData'. Open
$default_charset = App\Config::main('default_charset');
- 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\Config' in method 'read'. Open
$defaultCharset = App\Config::main('default_charset');
- 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 "file_encoding" 3 times. Open
$data[$label] = $this->convertCharacterEncoding($value, $this->request->get('file_encoding'), $default_charset);
- 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.
Avoid unused parameters such as '$hasHeader'. Open
public function getFirstRowData($hasHeader = true)
- Read upRead up
- Exclude checks
UnusedFormalParameter
Since: 0.2
Avoid passing parameters to methods or constructors and then not using those parameters.
Example
class Foo
{
private function bar($howdy)
{
// $howdy is not used
}
}
Source https://phpmd.org/rules/unusedcode.html#unusedformalparameter
Call to deprecated function \Import_VCardReader_Reader::convertCharacterEncoding()
defined at /code/modules/Import/readers/FileReader.php:94
Open
$data[$label] = $this->convertCharacterEncoding($value, $this->request->get('file_encoding'), $default_charset);
- Exclude checks
Call to deprecated function \Import_VCardReader_Reader::convertCharacterEncoding()
defined at /code/modules/Import/readers/FileReader.php:94
Open
$mappedData[$fieldName] = $this->convertCharacterEncoding($fieldValue, $this->request->get('file_encoding'), $defaultCharset);
- Exclude checks
Each class must be in a namespace of at least one level (a top-level vendor name) Open
class Import_VCardReader_Reader extends Import_FileReader_Reader
- Exclude checks
The class Import_VCardReader_Reader is not named in CamelCase. Open
class Import_VCardReader_Reader extends Import_FileReader_Reader
{
protected $vCardPattern = '/BEGIN:VCARD.*?END:VCARD/si';
protected $skipLabels = ['BEGIN', 'END', 'VERSION'];
public static $fileContents = null;
- 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
protected $skipLabels = ['BEGIN', 'END', 'VERSION'];
- Exclude checks
Line exceeds 120 characters; contains 128 characters Open
$data[$label] = $this->convertCharacterEncoding($value, $this->request->get('file_encoding'), $default_charset);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$fieldValueMappings = explode("\r\n", $row);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public static $fileContents = null;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (empty(self::$fileContents)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$data[$valueCounter++] = $value;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($this->request->get('file_encoding') !== $defaultCharset) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$allValuesEmpty = false;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach ($fieldValueMappings as $fieldValueMapping) {
- 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 ($fieldMapping as $fieldName => $index) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$mappedData[$fieldName] = $this->convertCharacterEncoding($fieldValue, $this->request->get('file_encoding'), $defaultCharset);
- 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->addRecordToDB($mappedData);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
preg_match_all($this->vCardPattern, self::$fileContents, $matches);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$matches = [];
- 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
$data = null;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$matches = [];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$value = str_replace(';', ' ', $value);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* Function creates tables for import in database.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
protected $vCardPattern = '/BEGIN:VCARD.*?END:VCARD/si';
- 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
$row = $matches[0][0];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$data = [];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
[$label, $value] = explode(':', $fieldValueMapping, 2);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$defaultCharset = App\Config::main('default_charset');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$this->createTable();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach ($fieldValueMappings as $fieldValueMapping) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$allValuesEmpty = true;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$mappedData[$fieldName] = $fieldValue;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$filePath = $this->getFilePath();
- 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
$filePath = $this->getFilePath();
- 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 (!\in_array($label, $this->skipLabels)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$data[$label] = $this->convertCharacterEncoding($value, $this->request->get('file_encoding'), $default_charset);
- 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 (empty(self::$fileContents)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$valueCounter = 0;
- 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
$default_charset = App\Config::main('default_charset');
- 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
self::$fileContents = file_get_contents($filePath);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$data = [];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (!\in_array($label, $this->skipLabels)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$fieldValueMappings = explode("\r\n", $row);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$value = str_replace(';', ' ', $value);
- Exclude checks
Line exceeds 120 characters; contains 146 characters Open
$mappedData[$fieldName] = $this->convertCharacterEncoding($fieldValue, $this->request->get('file_encoding'), $defaultCharset);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($allValuesEmpty) {
- 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
for ($i = 0; $i < $countMatches; ++$i) {
- 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
return true;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public function getFirstRowData($hasHeader = true)
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
self::$fileContents = file_get_contents($filePath);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public function 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
public function hasHeader()
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
[$label, $value] = explode(':', $fieldValueMapping, 2);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$fieldValue = $data[$index];
- 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 $data;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$fieldMapping = $this->request->get('field_mapping');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
preg_match_all($this->vCardPattern, self::$fileContents, $matches);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$countMatches = \count($matches[0]);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$row = $matches[0][$i];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$mappedData = [];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (!empty($fieldValue)) {
- Exclude checks
Class name "Import_VCardReader_Reader" is not in camel caps format Open
class Import_VCardReader_Reader extends Import_FileReader_Reader
- Exclude checks
The variable $default_charset is not named in camelCase. Open
public function getFirstRowData($hasHeader = true)
{
$default_charset = App\Config::main('default_charset');
$filePath = $this->getFilePath();
- Read upRead up
- Exclude checks
CamelCaseVariableName
Since: 0.2
It is considered best practice to use the camelCase notation to name variables.
Example
class ClassName {
public function doSomething() {
$data_module = new DataModule();
}
}
Source
The variable $default_charset is not named in camelCase. Open
public function getFirstRowData($hasHeader = true)
{
$default_charset = App\Config::main('default_charset');
$filePath = $this->getFilePath();
- Read upRead up
- Exclude checks
CamelCaseVariableName
Since: 0.2
It is considered best practice to use the camelCase notation to name variables.
Example
class ClassName {
public function doSomething() {
$data_module = new DataModule();
}
}