ShortList::isBrowser()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
/**
4
 * @file
5
 * This is the model for the short list URLs.
6
 */
7
8
/**
9
 * A model to store the short list of items and a unique URL.
10
 */
11
class ShortList extends DataObject
12
{
13
14
    /**
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
15
     * Fields.
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
16
     */
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
17
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
18
        'SessionID'     => 'varchar(64)',
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
19
        'URL'           => 'Varchar(255)',
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
20
        'UserAgent'     => 'Varchar(512)'
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
21
    );
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
22
23
    /**
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
24
     * Attaches to many items.
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
25
     */
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
26
    private static $has_many = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
27
        'ShortListItems' => 'ShortListItem'
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
28
    );
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
29
30
    /**
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
31
     * Where the URL is unique.
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
32
     */
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
33
    private static $indexes = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
34
        'UniqueURL'     => array(
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
35
            'type'      => 'unique',
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
36
            'value'     => 'URL'
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
37
        )
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
38
    );
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
39
40
    /**
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
41
     * Ensure we populate these fields before a save.
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
42
     */
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
43
    public function onBeforeWrite()
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
44
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
45
46
        // Run other beforewrites first.
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
47
        parent::onBeforeWrite();
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
48
49
        // if (!$this->isBrowser()) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
50
        //     return false;
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
51
        // }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
52
53
        // If this is the first save...
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
54
        if (!$this->isInDB()) {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
55
            // Ensure the session exists before querying it.
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
56
            if (!Session::request_contains_session_id()) {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
57
                Session::start();
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
58
            }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
59
60
            // Store the sesion and in the database.
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
61
            $this->SessionID = SecurityToken::inst()->getValue();
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
62
63
            if (is_null($this->SessionID)) {
0 ignored issues
show
Documentation introduced by
The property SessionID does not exist on object<ShortList>. 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...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
64
                return false;
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
65
            }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
66
67
            $gen = new RandomGenerator();
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
68
            $uniqueurl = substr($gen->randomToken(), 0, 32);
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
69
70
            while (ShortList::get()->filter('URL', $uniqueurl)->count() > 0) {
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
71
                $uniqueurl = substr($gen->randomToken(), 0, 32);
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
72
            }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
73
74
            $this->URL = $uniqueurl;
0 ignored issues
show
Documentation introduced by
The property URL does not exist on object<ShortList>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
75
            $this->UserAgent = Controller::curr()->getRequest()->getHeader('User-Agent');
0 ignored issues
show
Documentation introduced by
The property UserAgent does not exist on object<ShortList>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
76
        }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
77
    }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
78
79
    /**
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
80
     * All URLs are prefixed with URLSegment from config.
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
81
     */
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
82
    public function Link($action = null)
0 ignored issues
show
Unused Code introduced by
The parameter $action is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
83
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
84
        return Config::inst()->get('ShortList', 'URLSegment').$this->URL;
0 ignored issues
show
Documentation introduced by
The property URL does not exist on object<ShortList>. 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...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
85
    }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
86
87
    /**
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
88
     * A basic browser detection.
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
89
     * * if a browser is not here - assume it's a crawler or a bot. In which case, it shouldn't create a shortlist.
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
90
     *
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
91
     * @see http://stackoverflow.com/a/1537636
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
92
     * */
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
93
    public static function isBrowser()
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
94
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
95
        // Regular expression to match common browsers
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
96
        $browserlist = '/(opera|aol|msie|firefox|chrome|konqueror|safari|netscape|navigator|mosaic|lynx|amaya|omniweb|avant|camino|flock|seamonkey|mozilla|gecko)+/i';
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 166 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
97
98
        $userAgent = strtolower(Controller::curr()->getRequest()->getHeader('User-Agent'));
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
99
100
        return preg_match($browserlist, $userAgent) === 1;
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
101
    }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
102
}
103