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.

Activity::log()   F
last analyzed

Complexity

Conditions 17
Paths 1152

Size

Total Lines 57
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 57
rs 3.3193
cc 17
eloc 25
nc 1152
nop 1

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 namespace JackJoe\ActivityLog\Models;
2
3
use Illuminate\Database\Eloquent\Model as Eloquent;
4
use Illuminate\Support\Facades\Auth;
5
use Illuminate\Support\Facades\Request;
6
7
class Activity extends Eloquent {
8
  /**
9
   * The database table used by the model.
10
   *
11
   * @var string
12
   */
13
  protected $table = 'activity_log';
14
15
  /**
16
   * The fillable fields for the model.
17
   *
18
   * @var    array
19
   */
20
  protected $fillable = [
21
    'user_id',
22
    'content',
23
    'content_id',
24
    'action',
25
    'state',
26
    'details',
27
    'data',
28
    'version',
29
    'ip_address',
30
    'user_agent',
31
  ];
32
33
  /**
34
   * Get the user that the activity belongs to.
35
   *
36
   * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
37
   */
38
  public function user()
39
  {
40
    return $this->belongsTo(config('auth.providers.users.model'), 'user_id');
41
  }
42
43
  /**
44
   * Create an activity log entry.
45
   *
46
   * @param  mixed    $data
47
   * @return boolean
48
   */
49
  public static function log($data = [])
0 ignored issues
show
Coding Style introduced by
log uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
50
  {
51
    // set the defaults from config
52
    $defaults = config('activity-log.defaults');
53
    if (!is_array($defaults)) {
54
      $defaults = [];
55
    }
56
57
    if (is_object($data)) {
58
      $data = (array) $data;
59
    }
60
61
    // conversion path to content
62
    if (isset($data['contentType']) && !isset($data['content'])) {
63
      $data['content'] = $data['contentType'];
64
    }
65
66
    // conversion path to state
67
    if (isset($data['description']) && !isset($data['state'])) {
68
      $data['state'] = $data['description'];
69
    }
70
71
    // set the user ID
72
    if (config('activity-log.auto_set_user_id') && !isset($data['userId'])) {
73
      $user = call_user_func(config('activity-log.auth_method'));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
74
      $data['userId'] = isset($user->id) ? $user->id : null;
75
    }
76
77
    // set IP address
78
    if (!isset($data['ipAddress'])) {
79
      $data['ipAddress'] = Request::getClientIp();
80
    }
81
82
    // set user agent
83
    if (!isset($data['userAgent'])) {
84
      $data['userAgent'] = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'No User Agent';
85
    }
86
87
    // set additional data and encode it as JSON if it is an array or an object
88
    if (isset($data['data']) && (is_array($data['data']) || is_object($data['data']))) {
89
      $data['data'] = json_encode($data['data']);
90
    }
91
92
    // format array keys to snake case for insertion into database
93
    $dataFormatted = [];
94
    foreach ($data as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $data of type integer|double|string|array|boolean|resource is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
95
      $dataFormatted[snake_case($key)] = $value;
96
    }
97
98
    // merge defaults array with formatted data array
99
    $data = array_merge($defaults, $dataFormatted);
100
101
    // create the record
102
    $self = static::create($data);
103
104
    return $self;
105
  }
106
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
107