Skip to content

Commit c8cdeeb

Browse files
committed
Code quality improvements
1 parent f6b7418 commit c8cdeeb

File tree

10 files changed

+80
-53
lines changed

10 files changed

+80
-53
lines changed

Format/Arr.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function arrayKeys(): self
6666
*/
6767
public function arrayItemExpMerge(string $separator): self
6868
{
69-
$new = array();
69+
$new = [];
7070
foreach ($this->value as $item) {
7171
$exp = explode($separator, $item);
7272
$new = array_merge($new, $exp);

Format/Collection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __call($func, $argv)
3333

3434
} else {
3535
$copy = $this->value->getArrayCopy();
36-
$this->value = call_user_func_array($func, array_merge(array($copy), $argv));
36+
$this->value = call_user_func_array($func, array_merge([$copy], $argv));
3737

3838
}
3939

Format/DateTime.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class DateTime extends MainDateTime implements FormatInterface
3838
];
3939

4040
private $lang;
41-
private $translations = array();
41+
private $translations = [];
4242

4343

4444
/**

Format/Encode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function specialChar(bool $encode, int $flag = ENT_NOQUOTES): self
7474
public function encode(): string|array
7575
{
7676
// Always url decode first
77-
$this->value = $this->urldecode(function($value) {
77+
$this->value = $this->urldecode(function ($value) {
7878
$uri = Str::value((string)$value);
7979
if ($this->urlencode) {
8080
$uri->rawurlencode();

Format/Local.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ final class Local
1616
{
1717
protected static $prefix;
1818
protected static $dir;
19-
protected static $data = array();
19+
protected static $data = [];
2020

2121
protected $value;
22-
protected $sprint = array();
22+
protected $sprint = [];
2323

2424
/**
2525
* Init format by adding data to modify/format/traverse
@@ -97,7 +97,7 @@ public function get(string|array $key, string $fallback = "", ?array $sprint = n
9797
}
9898

9999
if (is_array($key)) {
100-
$out = array();
100+
$out = [];
101101
foreach ($key as $k) {
102102
$out[] = $this->getValue($k);
103103
}

Format/Num.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function toFilesize(): self
108108
$this->float();
109109
$precision = 2;
110110
$base = log($this->value) / log(1024);
111-
$suffixes = array('', ' kb', ' mb', ' g', ' t');
111+
$suffixes = ['', ' kb', ' mb', ' g', ' t'];
112112
$baseFloor = (int)floor($base);
113113
$suffix = (isset($suffixes[$baseFloor])) ? $suffixes[$baseFloor] : "";
114114
$this->value = round(pow(1024, $base - $baseFloor), $precision) . $suffix;

Format/Str.php

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace MaplePHP\DTO\Format;
1111

12+
use ErrorException;
1213
use InvalidArgumentException;
1314
use MaplePHP\DTO\MB;
1415

@@ -29,12 +30,6 @@ public function __construct(mixed $value)
2930
parent::__construct((string)$value);
3031
}
3132

32-
public static function test(): self
33-
{
34-
$value = ucfirst("dwq wqdw wdqdw dqw dwq");
35-
return new self($value);
36-
}
37-
3833
/**
3934
* Init format by adding data to modify/format/traverse
4035
* @param mixed $value
@@ -60,12 +55,12 @@ public function strVal(): string
6055
* @param integer $length total length
6156
* @param string $ending When break text add an ending (...)
6257
* @return self
63-
* @throws \ErrorException
58+
* @throws ErrorException
6459
*/
6560
public function excerpt(int $length = 40, string $ending = "..."): self
6661
{
6762
$this->stripTags()->entityDecode();
68-
$this->value = str_replace(array("'", '"', ""), array("", "", ""), $this->strVal());
63+
$this->value = str_replace(["'", '"', ""], ["", "", ""], $this->strVal());
6964
$mb = new MB($this->value);
7065
$strlen = $mb->strlen();
7166
$this->value = trim($mb->substr(0, $length));
@@ -122,7 +117,7 @@ public function specialChars(int $flag = ENT_QUOTES): self
122117
* Can be used to sanitize SQL identifiers that should be enclosed in backticks
123118
* @return self
124119
*/
125-
function sanitizeIdentifiers(): self
120+
public function sanitizeIdentifiers(): self
126121
{
127122
$this->value = preg_replace('/[^a-zA-Z0-9_-]/', '', $this->value);
128123
return $this;
@@ -163,7 +158,7 @@ public function clearBreaks(): self
163158
}
164159

165160
/**
166-
* Clear all white spaces characters incl.:
161+
* Clear all white spaces characters incl.:
167162
* spaces, tabs, newline characters, carriage returns, and form feed characters
168163
* @return self
169164
*/
@@ -281,12 +276,12 @@ public function formatSlug(): self
281276
*/
282277
public function replaceSpecialChar(): self
283278
{
284-
$pattern = array('é','è','ë','ê','É','È','Ë','Ê','á','à','ä','â','å','Á','À','Ä','Â','Å',
279+
$pattern = ['é','è','ë','ê','É','È','Ë','Ê','á','à','ä','â','å','Á','À','Ä','Â','Å',
285280
'ó','ò','ö','ô','Ó','Ò','Ö','Ô','í','ì','ï','î','Í','Ì','Ï','Î','ú','ù','ü','û','Ú',
286-
'Ù','Ü','Û','ý','ÿ','Ý','ø','Ø','œ','Œ','Æ','ç','Ç');
287-
$replace = array('e','e','e','e','E','E','E','E','a','a','a','a','a','A','A','A','A','A',
281+
'Ù','Ü','Û','ý','ÿ','Ý','ø','Ø','œ','Œ','Æ','ç','Ç'];
282+
$replace = ['e','e','e','e','E','E','E','E','a','a','a','a','a','A','A','A','A','A',
288283
'o','o','o','o','O','O','O','O','i','i','i','I','I','I','I','I','u','u','u','u','U',
289-
'U','U','U','y','y','Y','o','O','a','A','A','c','C');
284+
'U','U','U','y','y','Y','o','O','a','A','A','c','C'];
290285
$this->value = str_replace($pattern, $replace, $this->strVal());
291286

292287
return $this;
@@ -367,7 +362,7 @@ public function toggleUrlEncode(?array $find = null, ?array $replace = null): se
367362
{
368363
return $this->urldecode()->rawurlencode($find, $replace);
369364
}
370-
365+
371366
/**
372367
* Explode return array instance
373368
* @param non-empty-string $delimiter
@@ -448,9 +443,9 @@ public function toBool(): bool
448443
}
449444
return ($this->value !== "false" && strlen($this->value));
450445
}
451-
446+
452447
/**
453-
* Compare value to value
448+
* Compare value to value
454449
* @param string|int|float|bool|null $compare
455450
* @return bool
456451
*/

Iconv.php

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
*
77
* The polyfill map files comes from Symfony but the library itself has been completely re-built
88
*/
9+
910
namespace MaplePHP\DTO;
1011

1112
use ErrorException;
1213
use ValueError;
14+
1315
use function strlen;
1416

1517
class Iconv
@@ -45,7 +47,7 @@ public function __toString(): string
4547
* Get value
4648
* @return string|false
4749
*/
48-
function getValue(): string|false
50+
public function getValue(): string|false
4951
{
5052
return $this->value;
5153
}
@@ -92,7 +94,7 @@ public function encode(string $fromEncoding, string $toEncoding): self
9294
throw new ErrorException('iconv_strlen(): Wrong encoding, conversion from "' . $fromEncoding . '"');
9395
}
9496

95-
} else if ('utf-8' === $toEncoding) {
97+
} elseif ('utf-8' === $toEncoding) {
9698
// UTF-8 validation
9799
$inst->value = $inst->getSanitizedUTF8String($inst->value);
98100
}
@@ -126,7 +128,7 @@ protected function setEncoding(?string $encoding = null): self
126128
* @return int|false
127129
* @throws ErrorException
128130
*/
129-
function strlen(?string $encoding = null): int|false
131+
public function strlen(?string $encoding = null): int|false
130132
{
131133
$inst = $this->setEncoding($encoding);
132134
if(function_exists("iconv_strlen") && !$inst->disableVanilla) {
@@ -165,8 +167,8 @@ public function substr(int $start, ?int $length = null, ?string $encoding = null
165167
$inst = $inst->encode("utf-8", $inst->encoding);
166168
}
167169
$inc = 0;
168-
$inst->loop($inst->value, function($character, $charCount) use (&$value, &$inc, $start, $length) {
169-
if(($charCount+1) > $start) {
170+
$inst->loop($inst->value, function ($character, $charCount) use (&$value, &$inc, $start, $length) {
171+
if(($charCount + 1) > $start) {
170172
$value .= $character;
171173
$inc++;
172174
if($inc >= $length) {
@@ -192,7 +194,7 @@ public function substr(int $start, ?int $length = null, ?string $encoding = null
192194
* @return false|int
193195
* @throws ErrorException
194196
*/
195-
function strpos(string $needle, int $offset = 0, ?string $encoding = null): false|int
197+
public function strpos(string $needle, int $offset = 0, ?string $encoding = null): false|int
196198
{
197199

198200
$inst = $this->setEncoding($encoding);
@@ -203,7 +205,7 @@ function strpos(string $needle, int $offset = 0, ?string $encoding = null): fals
203205
return iconv_strpos($inst->value, $needle, $offset, $inst->encoding);
204206
}
205207
if(is_string($encoding)) {
206-
$inst = $inst->encode("utf-8", $inst->encoding);
208+
$inst = $inst->encode("utf-8", $inst->encoding);
207209
}
208210
$needleInst = new self($needle);
209211
if(is_string($encoding)) {
@@ -215,17 +217,22 @@ function strpos(string $needle, int $offset = 0, ?string $encoding = null): fals
215217
$offset = ($inst->strlen() + $offset);
216218
}
217219

218-
$inst->loop($inst->value, function(string $character, int $charCount) use (
219-
&$inc, &$total, &$completed,
220-
$needleLength, $needleInst, $offset, $encoding
221-
) {
222-
223-
if(($charCount+1) > $offset) {
220+
$inst->loop($inst->value, function (string $character, int $charCount) use (
221+
&$inc,
222+
&$total,
223+
&$completed,
224+
$needleLength,
225+
$needleInst,
226+
$offset,
227+
$encoding
228+
) {
229+
230+
if(($charCount + 1) > $offset) {
224231
$char = (string)$needleInst->substr($inc, 1);
225232
if($character === $char) {
226233
$inc++;
227234
if($inc === $needleLength) {
228-
$completed = ($charCount+1)-$inc;
235+
$completed = ($charCount + 1) - $inc;
229236
if(!$this->strposFollowThrough) {
230237
return $completed;
231238
}
@@ -253,7 +260,7 @@ function strpos(string $needle, int $offset = 0, ?string $encoding = null): fals
253260
* @return false|int
254261
* @throws ErrorException
255262
*/
256-
function strrpos(string $needle, ?string $encoding = null): false|int
263+
public function strrpos(string $needle, ?string $encoding = null): false|int
257264
{
258265
$inst = $this->setEncoding($encoding);
259266
$inst = $inst->strposFollowThrough(true);

MB.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function __toString(): string
3939
* Get value
4040
* @return string|false
4141
*/
42-
function getValue(): string|false
42+
public function getValue(): string|false
4343
{
4444
return $this->value;
4545
}
@@ -121,7 +121,7 @@ public function substr(int $start, ?int $length = null, ?string $encoding = null
121121
* @return false|int
122122
* @throws ErrorException
123123
*/
124-
function strpos(string $needle, int $offset = 0, ?string $encoding = null): false|int
124+
public function strpos(string $needle, int $offset = 0, ?string $encoding = null): false|int
125125
{
126126
if(function_exists('mb_strpos') && !$this->disableVanilla) {
127127
return mb_strpos($this->value, $needle, $offset, $encoding);
@@ -137,11 +137,11 @@ function strpos(string $needle, int $offset = 0, ?string $encoding = null): fals
137137
* @return false|int
138138
* @throws ErrorException
139139
*/
140-
function strrpos(string $needle, ?string $encoding = null): false|int
140+
public function strrpos(string $needle, ?string $encoding = null): false|int
141141
{
142142
if(function_exists('mb_strrpos') && !$this->disableVanilla) {
143143
return mb_strrpos($this->value, $needle, 0, $encoding);
144144
}
145145
return $this->iconv->strrpos($needle, $encoding);
146146
}
147-
}
147+
}

Traverse.php

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ class Traverse extends DynamicDataAbstract
3030
protected mixed $raw = null; // Use raw to access current instance data (access array)
3131
protected $data = null;
3232

33+
public function __construct(mixed $data = null)
34+
{
35+
parent::__construct();
36+
$this->build($data);
37+
}
38+
39+
/**
40+
* With new object
41+
* @param mixed $data
42+
* @return $this
43+
*/
44+
public function with(mixed $data): self
45+
{
46+
return new self($data);
47+
}
48+
3349
/**
3450
* Object traverser
3551
* @param $key
@@ -147,7 +163,7 @@ public function jsonDecode(): self
147163
public function toArray(?callable $callback = null): array
148164
{
149165
$index = 0;
150-
$new = array();
166+
$new = [];
151167
$inst = clone $this;
152168

153169
if (is_null($inst->raw)) {
@@ -173,7 +189,7 @@ public function toArray(?callable $callback = null): array
173189
public function fetch(?callable $callback = null): array|object|null
174190
{
175191
$index = 0;
176-
$new = array();
192+
$new = [];
177193
$inst = clone $this;
178194

179195
if (is_null($inst->raw)) {
@@ -279,19 +295,28 @@ protected function format(string $dtoClassName, mixed $value): object
279295
}
280296

281297
/**
282-
* Init instance
298+
* Build the object
283299
* @param mixed $data
284-
* @return self
300+
* @return $this
285301
*/
286-
public static function value(mixed $data): self
302+
protected function build(mixed $data): self
287303
{
288-
$inst = new self();
289304
if (is_array($data) || is_object($data)) {
290305
foreach ($data as $k => $v) {
291-
$inst->{$k} = $v;
306+
$this->{$k} = $v;
292307
}
293308
}
294-
$inst->raw = $data;
295-
return $inst;
309+
$this->raw = $data;
310+
return $this;
311+
}
312+
313+
/**
314+
* Init instance
315+
* @param mixed $data
316+
* @return self
317+
*/
318+
public static function value(mixed $data): self
319+
{
320+
return new self($data);
296321
}
297322
}

0 commit comments

Comments
 (0)