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.

Nip_Helper_Strings::limitWords()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 3
dl 0
loc 16
ccs 0
cts 9
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
class Nip_Helper_Strings extends Nip\Helpers\AbstractHelper {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
3
4
    /**
5
     * Limits a string to a certain number of words
6
     *
7
     * @param string $string
8
     * @param int $limit
9
     * @param string $end
10
     * @return string
11
     */
12
    public function limitWords($string, $limit = false, $end = '...') {
13
        $words = explode(" ", $string);
14
        
15
        if (count($words) <= $limit) {
16
            return $string;
17
        }
18
19
        $return = [];
20
        for ($i = 0; $i < $limit; $i++) {
21
            $return[] = $words[$i];
22
        }
23
24
        $return[] = $end;
25
26
        return implode(" ", $return);
27
    }
28
29
30
    /**
31
     * Injects GET params in links
32
     * 
33
     * @param string $string
34
     * @param array $params
35
     * @return string
36
     */
37
    public function injectParams($string, $params = array()) {
38
        $links = preg_split('#(<a\b[^>]+>)#', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
39
        $old   = $links;
40
41
        foreach ($links as &$match) {
42
            if (preg_match('/<a\b/', $match) && !preg_match('/(?:#|mailto)/', $match)) {
43
                preg_match('/^([^"]+")([^"]+)/', $match, $matches);
44
                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...
45
                    $link = html_entity_decode($matches[2]);
46
                    if (strpos($link, "?") === false) {
47
                        $link .= "?";
48
                    } else {
49
                        $link .= "&";
50
                    }
51
52
                    $link .= http_build_query($params);
53
54
                    $match = str_replace($matches[2], $link, $match);
55
                }
56
            }
57
        }
58
59
        $string = str_replace($old, $links, $string);
60
        return $string;
61
    }
62
63
64
    /**
65
     * Converts all relative hrefs and image srcs to absolute
66
     *
67
     * @param string $string
68
     * @param string $base
69
     * @return string
70
     */
71
    public function relativeToAbsolute($string, $base) {
72
        $matches = preg_split('#(<(a|img)\b[^>]+>)#', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
73
        $old     = $matches;
74
75
        foreach ($matches as &$match) {
76
            if (preg_match('/<(a|img)\b/', $match) && !preg_match('/(?:http|#|mailto)/', $match)) {
77
                $match = preg_replace('/^([^"]+")([^"]+)/', '$1'.$base.'$2', $match);
78
            }
79
        }
80
81
        $string = str_replace($old, $matches, $string);
82
        return $string;
83
    }
84
85
	public function moneyFormat($number)
86
	{
87
		return money_format('%n', $number);
88
	}
89
	
90
	public function cronoTimeInSeconds($time)
91
	{
92
		$parts = explode(':', $time);		
93
		$seconds = array_pop($parts);
94
		$minutes = array_pop($parts);
95
		$hours = array_pop($parts);
96
		$days = array_pop($parts);
97
98
		return (($days*24 + $hours)*60 + $minutes)*60 + $seconds;		
99
	}
100
	
101
	public function secondsInCronoTime($seconds)
102
	{
103 View Code Duplication
		if ($days = intval((floor($seconds / 86400)))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
            $seconds = $seconds - $days*86400;
105
			$return .= ($return ? ':' : '') . str_pad($days, 2, 0,STR_PAD_LEFT);
0 ignored issues
show
Bug introduced by
The variable $return does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
106
		}
107 View Code Duplication
		if ($hours = intval((floor($seconds / 3600))) OR $return) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
108
            $seconds = $seconds - $hours*3600;
109
			$return .= ($return ? ':' : '') . str_pad($hours, 2, 0,STR_PAD_LEFT);
110
		}
111 View Code Duplication
		if ($minutes = intval((floor($seconds / 60))) OR $return) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
112
			$seconds = $seconds - $minutes*60;
113
			$return .= ($return ? ':' : '') . str_pad($minutes, 2, 0, STR_PAD_LEFT);
114
		}
115
		$seconds = round($seconds, 2);  
116
		$return .= ($return ? ':' : '') . str_pad($seconds, 2, 0, STR_PAD_LEFT);
117
			
118
		return $return;
119
	}
120
}