Completed
Push — master ( ef76cc...010027 )
by Garrett
02:18
created

AnyString::concat()   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 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
    /**
31
     * @param mixed $thing Anything that can be cast to a string
32
     */
33
    public function __construct($thing)
34
    {
35
        self::testStringableObject($thing);
36
        $this->raw = (string) $thing;
37
    }
38
39
    /**
40
     * @return mixed
41
     */
42
    public function __get($name)
43
    {
44
        return $this->$name;
45
    }
46
47
    /**
48
     * @return string
49
     */
50
    public function __toString()
51
    {
52
        return $this->raw;
53
    }
54
55
    // ArrayAccess methods {
56
57
    public function offsetExists($offset)
58
    {
59
        $offset = (int) $offset;
60
        return ($offset >= 0 && $offset < $this->count());
61
    }
62
63
    // END ArrayAccess methods }
64
65
    // Iterator methods {
66
67
    public function key()
68
    {
69
        return $this->caret;
70
    }
71
72
    public function next()
73
    {
74
        $this->caret++;
75
    }
76
77
    public function rewind()
78
    {
79
        $this->caret = 0;
80
    }
81
82
    public function valid()
83
    {
84
        return ($this->caret < $this->count());
85
    }
86
87
    // END Iterator methods }
88
89
    public function equals($str)
90
    {
91
        self::testStringableObject($str);
92
93
        $str = (string) $str;
94
        return ($str == $this->raw);
95
    }
96
97
    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...
98
99
    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...
100
101
    /**
102
     * @return array
103
     */
104
    abstract public function toArray($delim = '', $limit = null);
105
106
    abstract public function append($str);
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...
107
108
    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...
109
110
    public function concat($str)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
111
    {
112
        return $this->append($str);
113
    }
114
115
    /**
116
     * @return AnyString
117
     */
118
    abstract public function escape($mode = self::NORMAL, $charlist = '');
119
120
    public function hexDecode()
121
    {
122
        return $this->replaceWhole(\hex2bin($this->raw));
123
    }
124
125
    public function hexEncode()
126
    {
127
        return $this->replaceWhole(\bin2hex($this->raw));
128
    }
129
130
    public function nextToken($delim)
131
    {
132
        if ($this->token) {
0 ignored issues
show
Documentation introduced by
The property token does not exist on object<StringObject\AnyString>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
133
            return new static(\strtok($delim));
134
        }
135
        $this->token = true;
0 ignored issues
show
Bug introduced by
The property token does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
136
        return new static(\strtok($this->raw, $delim));
137
    }
138
139
    abstract public function prepend($str);
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 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...
142
143
    abstract public function removeSubstr($start, $length = 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...
144
145
    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...
146
147
    /**
148
     * @param string $replace
149
     */
150
    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...
151
152
    abstract public function replaceSubstr($replacement, $start, $length = 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...
153
154
    /**
155
     * @return AnyString
156
     */
157
    public function replaceWhole($replacement = '')
158
    {
159
        return new static($replacement);
160
    }
161
162
    public function resetToken()
163
    {
164
        $this->token = false;
165
    }
166
167
    public function times($times)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
168
    {
169
        return $this->repeat($times);
170
    }
171
172
    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...
173
174
    /**
175
     * @return AnyString
176
     */
177
    abstract public function trim($mask = " \t\n\r\0\x0B", $mode = self::BOTH_ENDS);
178
179
    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...
180
181
    public function uuDecode()
182
    {
183
        return $this->replaceWhole(\convert_uudecode($this->raw));
184
    }
185
186
    public function uuEncode()
187
    {
188
        return $this->replaceWhole(\convert_uuencode($this->raw));
189
    }
190
191
    protected static function testStringableObject($thing)
192
    {
193
        if (is_string($thing)) {
194
            return true;
195
        }
196
197
        if (\is_object($thing) && !\method_exists($thing, '__toString')) {
198
            throw new \InvalidArgumentException(
199
                'Parameter is an object that does not implement __toString() method'
200
            );
201
        } elseif (\is_array($thing)) {
202
            throw new \InvalidArgumentException('Unsure of how to convert array to string');
203
        }
204
    }
205
}
206