autowp/image

View on GitHub
src/Sampler.php

Summary

Maintainability
F
6 days
Test Coverage
B
84%

Showing 43 of 43 total issues

File Sampler.php has 558 lines of code (exceeds 250 allowed). Consider refactoring.
Open

<?php
 
declare(strict_types=1);
 
namespace Autowp\Image;
Severity: Major
Found in src/Sampler.php - About 1 day to fix

    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");
    Severity: Minor
    Found in src/Sampler.php - About 4 hrs to fix

    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");
    Severity: Major
    Found in src/Sampler.php - About 3 hrs to fix

      The class Sampler has an overall complexity of 128 which is very high. The configured complexity threshold is 50.
      Open

      class Sampler
      {
      /**
      * @throws ImagickException
      */
      Severity: Minor
      Found in src/Sampler.php by phpmd

      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');
      Severity: Minor
      Found in src/Sampler.php - About 3 hrs to fix

      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');
      Severity: Major
      Found in src/Sampler.php - About 2 hrs to fix

        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();
        Severity: Minor
        Found in src/Sampler.php - About 2 hrs to fix

          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();
          Severity: Minor
          Found in src/Sampler.php - About 2 hrs to fix

            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;
            Severity: Minor
            Found in src/Sampler.php - About 1 hr to fix

              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;
              Severity: Minor
              Found in src/Sampler.php - About 1 hr to fix

              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;
              Severity: Minor
              Found in src/Sampler.php - About 1 hr to fix

                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;
                Severity: Minor
                Found in src/Sampler.php - About 1 hr to fix

                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();
                Severity: Minor
                Found in src/Sampler.php - About 1 hr to fix

                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;
                Severity: Minor
                Found in src/Sampler.php - About 1 hr to fix

                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();
                Severity: Minor
                Found in src/Sampler.php - About 1 hr to fix

                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;
                Severity: Minor
                Found in src/Sampler.php - About 1 hr to fix

                  Method extendEdgeColor has 29 lines of code (exceeds 25 allowed). Consider refactoring.
                  Open

                  private function extendEdgeColor(ImagickPixelIterator $iterator): ?ImagickPixel
                  {
                  $sum = [
                  'r' => [],
                  'g' => [],
                  Severity: Minor
                  Found in src/Sampler.php - About 1 hr to fix

                    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
                    Severity: Minor
                    Found in src/Sampler.php - About 35 mins to fix

                      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 {
                      Severity: Minor
                      Found in src/Sampler.php - About 25 mins to fix

                      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");
                      Severity: Minor
                      Found in src/Sampler.php by phpmd

                      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");
                      Severity: Minor
                      Found in src/Sampler.php by phpmd

                      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');
                      Severity: Minor
                      Found in src/Sampler.php by phpmd

                      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');
                      Severity: Minor
                      Found in src/Sampler.php by phpmd

                      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");
                      Severity: Minor
                      Found in src/Sampler.php by phpmd

                      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
                      Severity: Minor
                      Found in src/Sampler.php by phpmd

                      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);
                      }
                      Severity: Minor
                      Found in src/Sampler.php by phpmd

                      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();
                      }
                      Severity: Minor
                      Found in src/Sampler.php by phpmd

                      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;
                      Severity: Minor
                      Found in src/Sampler.php by phpmd

                      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;
                      Severity: Minor
                      Found in src/Sampler.php by phpmd

                      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;
                      }
                      Severity: Minor
                      Found in src/Sampler.php by phpmd

                      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;
                      }
                      Severity: Minor
                      Found in src/Sampler.php by phpmd

                      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);
                      Severity: Minor
                      Found in src/Sampler.php by phpmd

                      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;
                      }
                      Severity: Minor
                      Found in src/Sampler.php by phpmd

                      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`");
                      }
                      Severity: Minor
                      Found in src/Sampler.php by phpmd

                      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);
                      Severity: Minor
                      Found in src/Sampler.php by phpmd

                      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);
                      }
                      Severity: Minor
                      Found in src/Sampler.php by phpmd

                      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;
                      }
                      Severity: Minor
                      Found in src/Sampler.php by phpmd

                      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);
                      Severity: Minor
                      Found in src/Sampler.php by phpmd

                      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();
                      }
                      Severity: Minor
                      Found in src/Sampler.php by phpmd

                      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);
                      }
                      Severity: Minor
                      Found in src/Sampler.php by phpmd

                      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);
                      Severity: Minor
                      Found in src/Sampler.php by phpmd

                      Identical blocks of code found in 2 locations. Consider refactoring.
                      Open

                      if ($format->isReduceOnly() && $sizeLess) {
                      // dont crop
                      if (! $heightLess) {
                      // resize by height
                      $scaleHeight = $formatHeight;
                      Severity: Major
                      Found in src/Sampler.php and 1 other location - About 6 hrs to fix
                      src/Sampler.php on lines 170..194

                      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);
                      Severity: Major
                      Found in src/Sampler.php and 1 other location - About 6 hrs to fix
                      src/Sampler.php on lines 111..136
                      Category
                      Status