Completed
Pull Request — master (#19)
by Pavel
02:03
created

Helper::dashesToCamelCase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
namespace App\Common;
3
4
class Helper
5
{
6
    /**
7
     * @param string  $string
8
     * @param bool    $capitalizeFirstChar
9
     *
10
     * @return string
11
     */
12
    public static function dashesToCamelCase($string, $capitalizeFirstChar = false)
13
    {
14
        return static::replace($string, '-', $capitalizeFirstChar);
0 ignored issues
show
Bug introduced by
Since replace() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of replace() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
15
    }
16
17
    /**
18
     * @param string $string
19
     * @param bool   $capitalizeFirstChar
20
     *
21
     * @return string
22
     */
23
    public static function underscoreToCamelCase($string, $capitalizeFirstChar = false)
24
    {
25
        return static::replace($string, '_', $capitalizeFirstChar);
0 ignored issues
show
Bug introduced by
Since replace() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of replace() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
26
    }
27
28
    /**
29
     * @param int $length
30
     *
31
     * @return string
32
     */
33
    public static function generateRandomString($length = 32)
34
    {
35
        $chars      = 'abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ023456789';
36
        $charsCount = strlen($chars);
37
38
        srand((double)microtime() * 1000000);
39
        $i     = 1;
40
        $token = '';
41
42
        while ($i <= $length) {
43
            $num = rand() % $charsCount;
44
            $tmp = substr($chars, $num, 1);
45
            $token .= $tmp;
46
            $i++;
47
        }
48
49
        return $token;
50
    }
51
52
    /**
53
     * @param string $string
54
     * @param string $symbol
55
     * @param bool   $capitalizeFirstChar
56
     *
57
     * @return mixed|string
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
58
     */
59
    private static function replace($string, $symbol, $capitalizeFirstChar = false)
60
    {
61
        $str = str_replace(' ', '', ucwords(str_replace($symbol, ' ', $string)));
62
63
        if (!$capitalizeFirstChar) {
64
            $str = lcfirst($str);
65
        }
66
67
        return $str;
68
    }
69
}
70