SystemController::show()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 9.6666
cc 1
eloc 5
nc 1
nop 1
crap 2
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Http\Requests\Frontend\SubmitUrlRequest;
6
use App\Link;
7
8
class SystemController extends Controller
9
{
10
11
    /**
12
     * Log the hit and redirect to the url.
13
     *
14
     * @param Link $link
15
     *
16
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
17
     */
18
    public function show(Link $link)
19
    {
20
        $link->hits()->create([
21
            'ip'         => request()->ip(),
22
            'user_agent' => request()->server('HTTP_USER_AGENT'),
23
        ]);
24
25
        return redirect($link->url);
0 ignored issues
show
Documentation introduced by
The property url does not exist on object<App\Link>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
26
    }
27
28
29
    /**
30
     * Show the form for creating a new resource.
31
     * @return \Illuminate\Http\Response
32
     */
33
    public function create()
34
    {
35
        return view('dashboard.links.create');
36
    }
37
38
39
    /**
40
     * Store a newly created resource in storage.
41
     *
42
     * @param \App\Http\Requests\Frontend\SubmitUrlRequest $request
43
     *
44
     * @return \Illuminate\Http\RedirectResponse
45
     */
46
    public function store(SubmitUrlRequest $request, Link $link)
47
    {
48
        // Todo checking for failure here.
49
50
        $link = $link->generate($request->url, auth()->user());
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
Documentation introduced by
The property url does not exist on object<App\Http\Requests...ntend\SubmitUrlRequest>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
51
52
        $html_link = '<a href="/'.$link->hash.'">Try it now</a>';
53
54
        return redirect()->route('url_create')->with('message', 'Url added created! '.$html_link);
55
    }
56
}
57