Completed
Push — master ( 7e250e...a18ce8 )
by Vladimir
06:08
created

Str::camel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AcquiroPay\Helpers;
6
7
class Str
8
{
9
    private static $snakeCache = [];
10
    private static $camelCache = [];
11
    private static $studlyCache = [];
12
13
    /**
14
     * Get the class "basename" of the given object / class.
15
     *
16
     * @param  string|object $class
17
     * @return string
18
     */
19
    public static function classBaseName($class): string
20
    {
21
        $class = is_object($class) ? get_class($class) : $class;
22
23
        return basename(str_replace('\\', '/', $class));
24
    }
25
26
    /**
27
     * Convert the given string to lower-case.
28
     *
29
     * @param  string $value
30
     * @return string
31
     */
32
    public static function lower($value): string
33
    {
34
        return mb_strtolower($value, 'UTF-8');
35
    }
36
37
    /**
38
     * Convert a value to studly caps case.
39
     *
40
     * @param  string  $value
41
     * @return string
42
     */
43
    public static function studly($value)
44
    {
45
        $key = $value;
46
        if (isset(static::$studlyCache[$key])) {
0 ignored issues
show
Bug introduced by
Since $studlyCache is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $studlyCache to at least protected.

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

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

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 { }

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

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

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
47
            return static::$studlyCache[$key];
0 ignored issues
show
Bug introduced by
Since $studlyCache is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $studlyCache to at least protected.

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

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

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 { }

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

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

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
48
        }
49
        $value = ucwords(str_replace(['-', '_'], ' ', $value));
50
51
        return static::$studlyCache[$key] = str_replace(' ', '', $value);
0 ignored issues
show
Bug introduced by
Since $studlyCache is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $studlyCache to at least protected.

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

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

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 { }

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

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

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
52
    }
53
54
    /**
55
     * Convert a value to camel case.
56
     *
57
     * @param  string $value
58
     * @return string
59
     */
60
    public static function camel($value): string
61
    {
62
        if (isset(static::$camelCache[$value])) {
0 ignored issues
show
Bug introduced by
Since $camelCache is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $camelCache to at least protected.

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

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

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 { }

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

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

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
63
            return static::$camelCache[$value];
0 ignored issues
show
Bug introduced by
Since $camelCache is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $camelCache to at least protected.

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

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

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 { }

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

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

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
64
        }
65
66
        return static::$camelCache[$value] = lcfirst(static::studly($value));
0 ignored issues
show
Bug introduced by
Since $camelCache is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $camelCache to at least protected.

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

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

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 { }

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

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

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
67
    }
68
69
    /**
70
     * Convert a string to snake case.
71
     *
72
     * @param  string $value
73
     * @param  string $delimiter
74
     * @return string
75
     */
76
    public static function snake($value, $delimiter = '_'): string
77
    {
78
        $key = $value;
79
        if (isset(static::$snakeCache[$key][$delimiter])) {
0 ignored issues
show
Bug introduced by
Since $snakeCache is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $snakeCache to at least protected.

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

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

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 { }

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

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

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
80
            return static::$snakeCache[$key][$delimiter];
0 ignored issues
show
Bug introduced by
Since $snakeCache is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $snakeCache to at least protected.

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

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

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 { }

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

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

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
81
        }
82
        if (!ctype_lower($value)) {
83
            $value = preg_replace('/\s+/u', '', ucwords($value));
84
            $value = static::lower(preg_replace('/(.)(?=[A-Z])/u', '$1'.$delimiter, $value));
85
        }
86
87
        return static::$snakeCache[$key][$delimiter] = $value;
0 ignored issues
show
Bug introduced by
Since $snakeCache is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $snakeCache to at least protected.

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

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

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 { }

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

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

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
88
    }
89
}
90