Completed
Push — master ( 3473e4...1b0f71 )
by Garrett
11:38
created

AnyString::testStringableObject()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 14
rs 8.8571
cc 5
eloc 8
nc 4
nop 1
1
<?php
2
3
namespace StringObject;
4
5
abstract class AnyString implements \ArrayAccess, \Countable, \Iterator
6
{
7
    // CONSTANTS
8
9
    const START = 0;
10
    const END = 1;
11
    const BOTH_ENDS = 2;
12
    const NORMAL = 0;
13
    const CASE_INSENSITIVE = 1;
14
    const REVERSE = 2;
15
    const EXACT_POSITION = 4;
16
    const CURRENT_LOCALE = 2;
17
    const NATURAL_ORDER = 4;
18
    const FIRST_N = 8;
19
    const C_STYLE = 1;
20
    const META = 2;
21
    const GREEDY = 0;
22
    const LAZY = 1;
23
24
    // PROPERTIES
25
26
    protected $raw;
27
    protected $caret = 0;
28
29
30
    public function __construct($thing)
31
    {
32
        self::testStringableObject($thing);
33
        $this->raw = (string) $thing;
34
    }
35
36
    /**
37
     * @return mixed
38
     */
39
    public function __get($name)
40
    {
41
        return $this->$name;
42
    }
43
44
    /**
45
     * @return string
46
     */
47
    public function __toString()
48
    {
49
        return $this->raw;
50
    }
51
52
    // ArrayAccess methods {
53
54
    public function offsetExists($offset)
55
    {
56
        $offset = (int) $offset;
57
        return ($offset >= 0 && $offset < $this->count());
58
    }
59
60
    // END ArrayAccess methods }
61
62
    // Iterator methods {
63
64
    public function key()
65
    {
66
        return $this->caret;
67
    }
68
69
    public function next()
70
    {
71
        $this->caret++;
72
    }
73
74
    public function rewind()
75
    {
76
        $this->caret = 0;
77
    }
78
79
    public function valid()
80
    {
81
        return ($this->caret < $this->count());
82
    }
83
84
    // END Iterator methods }
85
86
    abstract public function toArray($delim = '', $limit = null);
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
87
88
    abstract public function compareTo($str, $mode = self::NORMAL, $length = 1);
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
89
90
    /**
91
     * @return AnyString
92
     */
93
    abstract public function escape($mode = self::NORMAL, $charlist = '');
94
95
    abstract public function nextToken($delim);
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
96
97
    abstract public function remove($str, $mode = self::NORMAL);
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
98
99
    abstract public function repeat($times);
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
100
101
    /**
102
     * @param string $replace
103
     */
104
    abstract public function replace($search, $replace, $mode = self::NORMAL);
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
105
106
    /**
107
     * @return AnyString
108
     */
109
    public function replaceWhole($replacement = '')
110
    {
111
        return new static($replacement);
112
    }
113
114
    abstract public function resetToken();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
115
116
    abstract public function times($times);
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
117
118
    abstract public function translate($search, $replace = '');
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
119
120
    /**
121
     * @return AnyString
122
     */
123
    abstract public function trim($mask = " \t\n\r\0\x0B", $mode = self::BOTH_ENDS);
124
125
    abstract public function unescape($mode = self::NORMAL);
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
126
127
    abstract public function uuDecode();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
128
129
    abstract public function uuEncode();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
130
131
    public function equals($str)
132
    {
133
        self::testStringableObject($str);
134
135
        $str = (string) $str;
136
        return ($str == $this->raw);
137
    }
138
139
    abstract public function isAscii();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
140
141
    abstract public function isEmpty();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
142
143
    protected static function testStringableObject($thing)
144
    {
145
        if (is_string($thing)) {
146
            return true;
147
        }
148
149
        if (\is_object($thing) && !\method_exists($thing, '__toString')) {
150
            throw new \InvalidArgumentException(
151
                'Parameter is an object that does not implement __toString() method'
152
            );
153
        } elseif (\is_array($thing)) {
154
            throw new \InvalidArgumentException('Unsure of how to convert array to string');
155
        }
156
    }
157
}
158