Completed
Push — master ( 89c8a9...bb4dcf )
by Sergey
03:12
created

CsrfHelper   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 6
Bugs 3 Features 0
Metric Value
wmc 7
c 6
b 3
f 0
lcom 0
cbo 0
dl 0
loc 44
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getTokenFromFile() 0 15 4
A _parseLineForToken() 0 13 3
1
<?php
2
3
namespace seregazhuk\PinterestBot\Helpers;
4
5
class CsrfHelper
6
{
7
    const TOKEN_NAME = 'csrftoken';
8
9
    /**
10
     * Get a CSRF token from the given cookie file
11
     *
12
     * @param string $file
13
     * @return string
14
     */
15
    public static function getTokenFromFile($file)
16
    {
17
        if ( ! file_exists($file)) {
18
            return null;
19
        }
20
21
        foreach (file($file) as $line) {
22
23
            if ($token = self::_parseLineForToken($line)) {
24
                return $token;
25
            }
26
        }
27
28
        return null;
29
    }
30
31
    /**
32
     * @param string $line
33
     * @return bool
34
     */
35
    protected static function _parseLineForToken($line)
36
    {
37
        if (empty(strstr($line, self::TOKEN_NAME))) {
38
            return false;
39
        }
40
41
        preg_match('/' . self::TOKEN_NAME . '\s(\w*)/', $line, $matches);
42
        if ($matches) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $matches of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
43
            return $matches[1];
44
        }
45
46
        return false;
47
    }
48
}
49