GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ImageTrait::imageResize()   B
last analyzed

Complexity

Conditions 11
Paths 18

Size

Total Lines 81
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 49
dl 0
loc 81
c 0
b 0
f 0
rs 7.3166
cc 11
nc 18
nop 5

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Raystech\StarterKit\Traits;
4
use Image;
5
6
trait ImageTrait
7
{
8
9
  /*
10
   |------------------------------------------------------------
11
   | @param  mixed  $image
12
   | @param  int    $width
13
   | @param  int    $height
14
   | @param  string $mode ['constrain', 'expand']
15
   | @param  int    $quality [0-100]
16
   |
17
   | @return \Intervention\Image\Image
0 ignored issues
show
Bug introduced by
The type Intervention\Image\Image was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
   |------------------------------------------------------------
19
   */
20
  private function imageResize($image, $width, $height = null, $mode = 'expand', $quality = 85) {
21
    if($height == null && $mode == 'expand') {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $height of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison === instead.
Loading history...
22
      $height = $width;
23
    }
24
25
    $img    = Image::make($image->getRealPath());
26
    // $img = $image;
27
28
    $margin = 0;
29
30
    $img_width  = $img->width();
31
    $img_height = $img->height();
32
33
    /*
34
    *  canvas
35
    */
36
    $dimension  = $width;
37
    $desire_width = $width;
38
    $desire_height = $height;
39
40
    $vertical   = ($img_width < $img_height);
41
    $horizontal = ($img_width > $img_height);
42
    $square     = ($img_width = $img_height);
0 ignored issues
show
Unused Code introduced by
The assignment to $img_width is dead and can be removed.
Loading history...
43
44
    if($mode == 'expand') {
45
      if ($vertical) {
46
        $top = $bottom = $margin;
47
        $newHeight = ($dimension) - ($bottom + $top);
48
        $img->resize(null, $newHeight, function ($constraint) {
49
          $constraint->aspectRatio();
50
        });
51
52
      } else if ($horizontal) {
53
        $right = $left = $margin;
54
        $newWidth = ($dimension) - ($right + $left);
55
        $img->resize($newWidth, null, function ($constraint) {
56
          $constraint->aspectRatio();
57
        });
58
59
      } else if ($square) {
60
        $right = $left = $margin;
61
        $newWidth = ($dimension) - ($left + $right);
62
        $img->resize($newWidth, null, function ($constraint) {
63
          $constraint->aspectRatio();
64
        });
65
66
      }
67
68
      $img->resizeCanvas($dimension, $dimension, 'center', false, '#ffffff');
69
      $img->encode('jpg', $quality);
70
71
    } else if($mode == 'constrain') {
72
      if ($vertical) {
73
        $top = $bottom = $margin;
74
        $newHeight = ($desire_height) - ($bottom + $top);
75
        $img->resize(null, $newHeight, function ($constraint) {
76
          $constraint->aspectRatio();
77
        });
78
79
      } else if ($horizontal) {
80
        $right = $left = $margin;
81
        $newWidth = ($desire_width) - ($right + $left);
82
        $img->resize($newWidth, null, function ($constraint) {
83
          $constraint->aspectRatio();
84
        });
85
86
      } else if ($square) {
87
        $right = $left = $margin;
88
        $newWidth = ($desire_width) - ($left + $right);
89
        $img->resize($newWidth, null, function ($constraint) {
90
          $constraint->aspectRatio();
91
        });
92
93
      }
94
95
      $img->resizeCanvas($desire_width, $desire_height, 'center', false, '#ffffff');
96
      $img->encode('jpg', $quality);
97
    }
98
99
100
    return $img;
101
    // $img->save(public_path("storage/{$token}/{$origFilename}"));
102
  }
103
}