Function getSupportedDrivers
has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring. Open
public static function getSupportedDrivers(): array
{
$drivers = [];
foreach ((new \DirectoryIterator(__DIR__ . \DIRECTORY_SEPARATOR . 'Drivers')) as $fileInfo) {
$fileName = $fileInfo->getBasename('.php');
- 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 '56', column '17'). Open
foreach ((new \DirectoryIterator(__DIR__ . \DIRECTORY_SEPARATOR . 'Drivers')) 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 '116', column '17'). Open
$merger = new \setasign\Fpdi\Fpdi();
- 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 '\Vtiger_PDF_Model' in method 'getInstanceByTemplateId'. Open
$template = \Vtiger_PDF_Model::getInstanceById($templateId);
- 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\Log' in method 'getSupportedDrivers'. Open
\App\Log::warning('Not found custom class: ' . $className);
- 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\Language' in method 'getDriverLabel'. Open
$label = \App\Language::translate($className::DRIVER_NAME, 'Settings::PDF');
- 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 "\App\Pdf\Drivers\" 3 times. Open
$className = '\App\Pdf\Drivers\\' . $fileName;
- 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 Output
from undeclared class \setasign\Fpdi\Fpdi
Open
$merger->Output($path, $pdfFlag);
- Exclude checks
Call to method AddPage
from undeclared class \setasign\Fpdi\Fpdi
Open
$merger->AddPage(($size['width'] > $size['height']) ? 'L' : 'P', [$size['width'], $size['height']]);
- Exclude checks
Call to method warning
from undeclared class \App\Log
Open
\App\Log::warning('Not found custom class: ' . $className);
- Exclude checks
Call to method importPage
from undeclared class \setasign\Fpdi\Fpdi
Open
$template = $merger->importPage($i);
- Exclude checks
Call to method useTemplate
from undeclared class \setasign\Fpdi\Fpdi
Open
$merger->useTemplate($template);
- Exclude checks
Call to method setSourceFile
from undeclared class \setasign\Fpdi\Fpdi
Open
$pageCount = $merger->setSourceFile($file);
- Exclude checks
Call to method __construct
from undeclared class \setasign\Fpdi\Fpdi
Open
$merger = new \setasign\Fpdi\Fpdi();
- Exclude checks
Call to method getTemplateSize
from undeclared class \setasign\Fpdi\Fpdi
Open
$size = $merger->getTemplateSize($template);
- Exclude checks
Similar blocks of code found in 2 locations. Consider refactoring. Open
public static function getPageFormats(): array
{
return [
'4A0',
'2A0',
- 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 173.
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
Spaces must be used to indent lines; tabs are not allowed Open
* Get page formats.
- 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 getSupportedDrivers(): array
- 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 string[]
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'DEMY', // 'Demy' format paperback size 135x216mm
- 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
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'LEDGER',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$drivers = [];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach ((new \DirectoryIterator(__DIR__ . \DIRECTORY_SEPARATOR . 'Drivers')) as $fileInfo) {
- 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 getPageFormats(): array
- 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
'SRA0', 'SRA1', 'SRA2', 'SRA3', 'SRA4',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'FOLIO',
- 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
{
- 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
'4A0',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'B0', 'B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B9', 'B10',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'A', // 'A' format paperback size 111x178mm
- 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 ($className::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
* Get driver label.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'RA0', 'RA1', 'RA2', 'RA3', 'RA4',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
\App\Log::warning('Not found custom class: ' . $className);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9', 'C10',
- 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
'ROYAL', // 'Royal' format paperback size 153x234mm
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* Get supported drivers for generating PDF.
- 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
'2A0',
- 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
'LETTER',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'B', // 'B' format paperback size 128x198mm
- 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
$drivers[$fileName] = $className::DRIVER_NAME;
- 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
'A0', 'A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9', 'A10',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$className = '\App\Pdf\Drivers\\' . $fileName;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'EXECUTIVE',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return $drivers;
- 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
$pageCount = $merger->setSourceFile($file);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$merger->AddPage(($size['width'] > $size['height']) ? 'L' : 'P', [$size['width'], $size['height']]);
- 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
'LEGAL',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'TABLOID',
- 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
$fileName = $fileInfo->getBasename('.php');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public static function getDriverLabel(string $name): string
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ('Base' !== $fileName && 'php' === $fileInfo->getExtension()) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @return Drivers\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
* @param string $name
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* Function that merges PDF files into one.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @return void
- 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 getInstanceByTemplateId(int $templateId): Drivers\Base
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$merger = new \setasign\Fpdi\Fpdi();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach ($files as $file) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return $label;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @param int $templateId
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @param string $path File name or path to write
- 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 (!class_exists($className)) {
- 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 (class_exists($className)) {
- 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
$merger->Output($path, $pdfFlag);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$template = $merger->importPage($i);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$merger->useTemplate($template);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return $pdf;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @param string[] $files List of files to merge
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$label = \App\Language::translate($className::DRIVER_NAME, 'Settings::PDF');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @param string $pdfFlag Merge mode e.g. `I` = show in browser , `D` = download , `F` = save to file
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public static function merge(array $files, string $path, string $pdfFlag): void
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$size = $merger->getTemplateSize($template);
- 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\Pdf\Drivers\\' . $name;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$label = $name;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* Get PDF instance by template id.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$className = '\App\Pdf\Drivers\\' . $template->get('generator');
- 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 = 1; $i <= $pageCount; ++$i) {
- 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
$template = \Vtiger_PDF_Model::getInstanceById($templateId);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$pdf = new $className();
- 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
$pdf->setTemplate($template);
- 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