File Sampler.php
has 558 lines of code (exceeds 250 allowed). Consider refactoring. Open
<?php
declare(strict_types=1);
namespace Autowp\Image;
Function convertImagick
has a Cognitive Complexity of 31 (exceeds 5 allowed). Consider refactoring. Open
public function convertImagick(Imagick $imagick, ?array $crop, $format): Imagick
{
if (! $format instanceof Sampler\Format) {
if (! is_array($format)) {
throw new Sampler\Exception("Unexpected type of format");
- 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 convertImagick
has 84 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function convertImagick(Imagick $imagick, ?array $crop, $format): Imagick
{
if (! $format instanceof Sampler\Format) {
if (! is_array($format)) {
throw new Sampler\Exception("Unexpected type of format");
The class Sampler has an overall complexity of 128 which is very high. The configured complexity threshold is 50. Open
class Sampler
{
/**
* @throws ImagickException
*/
- Exclude checks
Function cropImage
has a Cognitive Complexity of 23 (exceeds 5 allowed). Consider refactoring. Open
private function cropImage(Imagick $imagick, array $crop, Sampler\Format $format): Imagick
{
$cropSet = isset($crop['width'], $crop['height'], $crop['left'], $crop['top']);
if (! $cropSet) {
throw new Sampler\Exception('Crop parameters not properly set');
- 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 cropImage
has 67 lines of code (exceeds 25 allowed). Consider refactoring. Open
private function cropImage(Imagick $imagick, array $crop, Sampler\Format $format): Imagick
{
$cropSet = isset($crop['width'], $crop['height'], $crop['left'], $crop['top']);
if (! $cropSet) {
throw new Sampler\Exception('Crop parameters not properly set');
Method extendHorizontal
has 50 lines of code (exceeds 25 allowed). Consider refactoring. Open
private function extendHorizontal(Imagick $imagick, Sampler\Format $format): void
{
$fRatio = (int) $format->getWidth() / (int) $format->getHeight();
$srcWidth = $imagick->getImageWidth();
Method extendVertical
has 50 lines of code (exceeds 25 allowed). Consider refactoring. Open
private function extendVertical(Imagick $imagick, Sampler\Format $format): void
{
$fRatio = (int) $format->getWidth() / (int) $format->getHeight();
$srcWidth = $imagick->getImageWidth();
Method convertByOuterFit
has 38 lines of code (exceeds 25 allowed). Consider refactoring. Open
private function convertByOuterFit(Imagick $imagick, Sampler\Format $format): Imagick
{
$srcWidth = $imagick->getImageWidth();
$srcHeight = $imagick->getImageHeight();
$srcRatio = $srcWidth / $srcHeight;
Function convertByInnerFit
has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring. Open
private function convertByInnerFit(Imagick $imagick, Sampler\Format $format): Imagick
{
$srcWidth = $imagick->getImageWidth();
$srcHeight = $imagick->getImageHeight();
$srcRatio = $srcWidth / $srcHeight;
- 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 convertByInnerFit
has 35 lines of code (exceeds 25 allowed). Consider refactoring. Open
private function convertByInnerFit(Imagick $imagick, Sampler\Format $format): Imagick
{
$srcWidth = $imagick->getImageWidth();
$srcHeight = $imagick->getImageHeight();
$srcRatio = $srcWidth / $srcHeight;
Function convertByMaximumFit
has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring. Open
private function convertByMaximumFit(Imagick $imagick, Sampler\Format $format): Imagick
{
$srcWidth = $imagick->getImageWidth();
$srcHeight = $imagick->getImageHeight();
$srcRatio = $srcWidth / $srcHeight;
- 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 extendVertical
has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring. Open
private function extendVertical(Imagick $imagick, Sampler\Format $format): void
{
$fRatio = (int) $format->getWidth() / (int) $format->getHeight();
$srcWidth = $imagick->getImageWidth();
- 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 convertByOuterFit
has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring. Open
private function convertByOuterFit(Imagick $imagick, Sampler\Format $format): Imagick
{
$srcWidth = $imagick->getImageWidth();
$srcHeight = $imagick->getImageHeight();
$srcRatio = $srcWidth / $srcHeight;
- 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 extendHorizontal
has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring. Open
private function extendHorizontal(Imagick $imagick, Sampler\Format $format): void
{
$fRatio = (int) $format->getWidth() / (int) $format->getHeight();
$srcWidth = $imagick->getImageWidth();
- 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 convertByMaximumFit
has 30 lines of code (exceeds 25 allowed). Consider refactoring. Open
private function convertByMaximumFit(Imagick $imagick, Sampler\Format $format): Imagick
{
$srcWidth = $imagick->getImageWidth();
$srcHeight = $imagick->getImageHeight();
$srcRatio = $srcWidth / $srcHeight;
Method extendEdgeColor
has 29 lines of code (exceeds 25 allowed). Consider refactoring. Open
private function extendEdgeColor(ImagickPixelIterator $iterator): ?ImagickPixel
{
$sum = [
'r' => [],
'g' => [],
Method crop
has 5 arguments (exceeds 4 allowed). Consider refactoring. Open
private function crop(Imagick $imagick, int $width, int $height, int $left, int $top): Imagick
Function convertToFile
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
public function convertToFile($source, string $destFile, $format): void
{
if ($source instanceof Imagick) {
$imagick = clone $source; // to prevent modifying source
} else {
- 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
The method convertImagick() has an NPath complexity of 110592. The configured NPath complexity threshold is 200. Open
public function convertImagick(Imagick $imagick, ?array $crop, $format): Imagick
{
if (! $format instanceof Sampler\Format) {
if (! is_array($format)) {
throw new Sampler\Exception("Unexpected type of format");
- Read upRead up
- Exclude checks
NPathComplexity
Since: 0.1
The NPath complexity of a method is the number of acyclic execution paths through that method. A threshold of 200 is generally considered the point where measures should be taken to reduce complexity.
Example
class Foo {
function bar() {
// lots of complicated code
}
}
Source https://phpmd.org/rules/codesize.html#npathcomplexity
The method convertImagick() has 107 lines of code. Current threshold is set to 100. Avoid really long methods. Open
public function convertImagick(Imagick $imagick, ?array $crop, $format): Imagick
{
if (! $format instanceof Sampler\Format) {
if (! is_array($format)) {
throw new Sampler\Exception("Unexpected type of format");
- Exclude checks
The method cropImage() has an NPath complexity of 1056. The configured NPath complexity threshold is 200. Open
private function cropImage(Imagick $imagick, array $crop, Sampler\Format $format): Imagick
{
$cropSet = isset($crop['width'], $crop['height'], $crop['left'], $crop['top']);
if (! $cropSet) {
throw new Sampler\Exception('Crop parameters not properly set');
- Read upRead up
- Exclude checks
NPathComplexity
Since: 0.1
The NPath complexity of a method is the number of acyclic execution paths through that method. A threshold of 200 is generally considered the point where measures should be taken to reduce complexity.
Example
class Foo {
function bar() {
// lots of complicated code
}
}
Source https://phpmd.org/rules/codesize.html#npathcomplexity
The method convertImagick() has a Cyclomatic Complexity of 25. The configured cyclomatic complexity threshold is 10. Open
public function convertImagick(Imagick $imagick, ?array $crop, $format): Imagick
{
if (! $format instanceof Sampler\Format) {
if (! is_array($format)) {
throw new Sampler\Exception("Unexpected type of format");
- Read upRead up
- Exclude checks
CyclomaticComplexity
Since: 0.1
Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.
Example
// Cyclomatic Complexity = 11
class Foo {
1 public function example() {
2 if ($a == $b) {
3 if ($a1 == $b1) {
fiddle();
4 } elseif ($a2 == $b2) {
fiddle();
} else {
fiddle();
}
5 } elseif ($c == $d) {
6 while ($c == $d) {
fiddle();
}
7 } elseif ($e == $f) {
8 for ($n = 0; $n < $h; $n++) {
fiddle();
}
} else {
switch ($z) {
9 case 1:
fiddle();
break;
10 case 2:
fiddle();
break;
11 case 3:
fiddle();
break;
default:
fiddle();
break;
}
}
}
}
Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity
The method cropImage() has a Cyclomatic Complexity of 20. The configured cyclomatic complexity threshold is 10. Open
private function cropImage(Imagick $imagick, array $crop, Sampler\Format $format): Imagick
{
$cropSet = isset($crop['width'], $crop['height'], $crop['left'], $crop['top']);
if (! $cropSet) {
throw new Sampler\Exception('Crop parameters not properly set');
- Read upRead up
- Exclude checks
CyclomaticComplexity
Since: 0.1
Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.
Example
// Cyclomatic Complexity = 11
class Foo {
1 public function example() {
2 if ($a == $b) {
3 if ($a1 == $b1) {
fiddle();
4 } elseif ($a2 == $b2) {
fiddle();
} else {
fiddle();
}
5 } elseif ($c == $d) {
6 while ($c == $d) {
fiddle();
}
7 } elseif ($e == $f) {
8 for ($n = 0; $n < $h; $n++) {
fiddle();
}
} else {
switch ($z) {
9 case 1:
fiddle();
break;
10 case 2:
fiddle();
break;
11 case 3:
fiddle();
break;
default:
fiddle();
break;
}
}
}
}
Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity
The method standardDeviation has a boolean flag argument $sample, which is a certain sign of a Single Responsibility Principle violation. Open
private function standardDeviation(array $values, bool $sample = false): float
- 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
The method convertImagick uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
if ($fWidth) {
$decomposited = $this->convertByWidth($decomposited, $format);
} elseif ($fHeight) {
$decomposited = $this->convertByHeight($decomposited, $format);
- 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 convertByMaximumFit uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
// добавляем поля по бокам
$scaleWidth = (int) round($formatHeight * $srcRatio);
$scaleHeight = $formatHeight;
}
- 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 convertImagick uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$decomposited = $imagick;
}
- 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 scaleImage uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$imagick->scaleImage($width, $height, false);
}
- 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 convertToFile uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$imagick = new Imagick();
if (! $imagick->readImage($source)) {
throw new Sampler\Exception("Error read image from `$source`");
}
- 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 crop uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$imagick->setImagePage(0, 0, 0, 0);
$imagick->cropImage($width, $height, $left, $top);
}
- 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 convertByMaximumFit uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
// высчитываем размеры обрезания
if ($ratio < $srcRatio) {
$scaleWidth = $formatWidth;
$scaleHeight = (int) round($formatWidth / $srcRatio);
- 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 convertByInnerFit uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
// высокая картинка
$cropWidth = $srcWidth;
$cropHeight = (int) round($srcWidth / $ratio);
$cropLeft = 0;
- 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 convertImagick uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$this->extendHorizontal($decomposited, $format);
}
- 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 convertByOuterFit uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
if ($ratio < $srcRatio) {
$scaleWidth = $formatWidth;
// add top and bottom margins
$scaleHeight = (int) round($formatWidth / $srcRatio);
- 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 convertImagick uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$imagick = $decomposited;
}
- 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 convertByInnerFit uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
// высчитываем размеры обрезания
if ($ratio < $srcRatio) {
// широкая картинка
$cropWidth = (int) round($srcHeight * $ratio);
- 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 convertByOuterFit uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
// add left and right margins
$scaleWidth = (int) round($formatHeight * $srcRatio);
$scaleHeight = $formatHeight;
}
- 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 convertByWidth uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$scaleWidth = (int) $format->getWidth();
}
- 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 cropImage uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
// crop higher than format, need more width
$targetWidth = (int) round($cropHeight * $fRatio);
if ($targetWidth > $width) {
$targetWidth = $width;
- 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 convertByHeight uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$scaleHeight = (int) $format->getHeight();
}
- 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
Identical blocks of code found in 2 locations. Consider refactoring. Open
if ($format->isReduceOnly() && $sizeLess) {
// dont crop
if (! $heightLess) {
// resize by height
$scaleHeight = $formatHeight;
- 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 205.
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
Identical blocks of code found in 2 locations. Consider refactoring. Open
if ($format->isReduceOnly() && $sizeLess) {
if (! $heightLess) {
// resize by height
$scaleHeight = $formatHeight;
$scaleWidth = (int) round($scaleHeight * $srcRatio);
- 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 205.
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