Passed
Pull Request — develop (#77)
by Glynn
02:37
created

similar()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 1
nop 2
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Composable strings functions.
5
 *
6
 * This file is part of PinkCrab Function Constructors.
7
 *
8
 * PinkCrab Function Constructors is free software: you can redistribute it and/or modify it under the terms of the
9
 * GNU General Public License as published by the Free Software Foundation, either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * PinkCrab Function Constructors is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
13
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 * See the GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with PinkCrab Function Constructors.
17
 * If not, see <https://www.gnu.org/licenses/>.
18
 *
19
 * @author Glynn Quelch <[email protected]>
20
 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
21
 * @package PinkCrab\FunctionConstructors
22
 * @since 0.0.1
23
 *
24
 * @template Number of int|float
25
 * @phpstan-template Number of int|float
26
 * @psalm-template Number of int|float
27
 */
28
29
declare(strict_types=1);
30
31
namespace PinkCrab\FunctionConstructors\Strings;
32
33
use Closure;
34
use PinkCrab\FunctionConstructors\Comparisons as C;
0 ignored issues
show
Bug introduced by
The type PinkCrab\FunctionConstructors\Comparisons was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
35
use PinkCrab\FunctionConstructors\GeneralFunctions as F;
0 ignored issues
show
Bug introduced by
The type PinkCrab\FunctionConstructors\GeneralFunctions was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
37
/**
38
 * Creates a callable for wrapping a string.
39
 * By defaults uses opening as closing, if no closing defined.
40
 *
41
 * @param string $opening
42
 * @param string|null $closing
43
 * @return Closure(string):string
44
 */
45
function wrap(string $opening, ?string $closing = null): Closure
46
{
47
    /**
48
     * @param string $string
49
     * @return string
50
     */
51
    return function (string $string) use ($opening, $closing): string {
52
        return \sprintf('%s%s%s', $opening, $string, $closing ?? $opening);
53
    };
54
}
55
56
/**
57
 * Creates a callable for slicing a string
58
 *
59
 * Uses substr()
60
 *
61
 * @param int $start start position
62
 * @param int|null $finish end position
63
 * @return Closure(string):string
64
 */
65
function slice(int $start, ?int $finish = null): Closure
66
{
67
    /**
68
     * @param string $string
69
     * @return string
70
     */
71
    return function (string $string) use ($start, $finish): string {
72
        return ! $finish
73
            ? substr($string, $start)
74
            : substr($string, $start, $finish);
75
    };
76
}
77
78
/**
79
 * Creates a callable for prepending to a string.
80
 *
81
 * @param string $prepend
82
 * @return Closure(string):string
83
 */
84
function prepend(string $prepend): Closure
85
{
86
    /**
87
     * @param string $string
88
     * @return string
89
     */
90
    return function (string $string) use ($prepend): string {
91
        return \sprintf('%s%s', $prepend, $string);
92
    };
93
}
94
95
/**
96
 * Creates a callable for appending to a string.
97
 *
98
 * @param string $append
99
 * @return Closure(string):string
100
 */
101
function append(string $append): Closure
102
{
103
    /**
104
     * @param string $string
105
     * @return string
106
     */
107
    return function (string $string) use ($append): string {
108
        return \sprintf('%s%s', $string, $append);
109
    };
110
}
111
112
/**
113
 * Returns a callable for formatting a string with a defined set of rules
114
 *
115
 * @param string $template
116
 * @return Closure(array<string, mixed>):string
117
 */
118
function vSprintf(string $template): Closure
119
{
120
    /**
121
     * @param array<string, mixed> $values
122
     * @return string Will return original string if false.
123
     */
124
    return function (array $values = array()) use ($template): string {
125
        return \vsprintf($template, $values);
126
    };
127
}
128
129
/**
130
 * Creates a double curried find to replace.
131
 *
132
 * @param string $find Value to look for
133
 * @return Closure(string):Closure
134
 */
135
function findToReplace(string $find): Closure
136
{
137
    /**
138
     * @param string $replace value to replace with
139
     * @return Closure(string):string
140
     */
141
    return function (string $replace) use ($find): Closure {
142
        /**
143
         * @param string $subject String to carry out find and replace.
144
         * @return string
145
         */
146
        return function ($subject) use ($find, $replace): string {
147
            return \str_replace($find, $replace, $subject);
148
        };
149
    };
150
}
151
152
/**
153
 * Creates a Closure to find and replace within a string.
154
 *
155
 * @param string  $find
156
 * @param string  $replace
157
 * @return Closure(string):string
158
 */
159
function replaceWith(string $find, string $replace): Closure
160
{
161
    /**
162
     * @param string $source
163
     * @return string
164
     */
165
    return function ($source) use ($find, $replace): string {
166
        return \str_replace($find, $replace, $source);
167
    };
168
}
169
170
/**
171
 * Returns a callable that can replace a sub string with a pre defined value.
172
 *
173
 * @param string $replace The value to replace in passed string
174
 * @param int $offset The offset to start, negative numbers count back from end.
175
 * @param int|null $length Number of chars to stop replacing at end of replacement.
176
 * @return Closure(string):string
177
 */
178
function replaceSubString(string $replace, int $offset = 0, ?int $length = null): Closure
179
{
180
    /**
181
     * @param string $string
182
     * @return string
183
     */
184
    return function (string $string) use ($replace, $offset, $length): string {
185
        return $length
0 ignored issues
show
Bug Best Practice introduced by
The expression return $length ? substr_...ing, $replace, $offset) could return the type array which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
186
            ? \substr_replace($string, $replace, $offset, $length)
187
            : \substr_replace($string, $replace, $offset);
188
    };
189
}
190
191
/**
192
 * Creates a callable for checking if a string starts with
193
 *
194
 * @param string $find The value to look for.
195
 * @return Closure(string):bool
196
 */
197
function startsWith(string $find): Closure
198
{
199
    /**
200
     * @param string $source
201
     * @return bool
202
     */
203
    return function (string $source) use ($find): bool {
204
        return (\substr($source, 0, \strlen($find)) === $find);
205
    };
206
}
207
208
/**
209
 * Creates a callable for checkin if a string ends with
210
 *
211
 * @param string $find The value to look for.
212
 * @return Closure(string):bool
213
 */
214
function endsWith(string $find): Closure
215
{
216
    /**
217
     * @param string $source
218
     * @return bool
219
     */
220
    return function (string $source) use ($find): bool {
221
        if (\strlen($find) === 0) {
222
            return true;
223
        }
224
        return (\substr($source, -\strlen($find)) === $find);
225
    };
226
}
227
228
/**
229
 * Creates a callable for checking if a string contains. using stringContains
230
 *
231
 * @param string $needle The value to look for.
232
 * @return Closure(string):bool
233
 */
234
function contains(string $needle): Closure
235
{
236
    /**
237
     * @param string $haystack String to look in.
238
     * @return bool
239
     */
240
    return function (string $haystack) use ($needle): bool {
241
        return \stringContains($haystack, $needle);
242
    };
243
}
244
245
/**
246
 * Creates a callable for checking if a string contains using preg_match.
247
 *
248
 * @param string $pattern
249
 * @return Closure(string):bool
250
 */
251
function containsPattern(string $pattern): Closure
252
{
253
    /**
254
     * @param string $source String to look in.
255
     * @return bool
256
     */
257
    return function (string $source) use ($pattern): bool {
258
        return (bool) \preg_match($pattern, $source);
259
    };
260
}
261
262
/**
263
 * Splits a string with a pattern
264
 *
265
 * @param string $pattern
266
 * @return Closure(string):?string[]
267
 */
268
function splitPattern(string $pattern): Closure
269
{
270
    /**
271
     * @param string $name
272
     * @return string[]
273
     */
274
    return function (string $string) use ($pattern): ?array {
275
        return \preg_split($pattern, $string) ?: null;
276
    };
277
}
278
279
/**
280
 * Converts a number (loose type) to a string representation of a float.
281
 *
282
 * @param string|int|float $precision Number of decimal places
283
 * @param string $point The decimal separator
284
 * @param string $thousands The thousand separator.
285
 * @return Closure(string|int|float):string
286
 */
287
function digit($precision = 2, $point = '.', $thousands = ''): Closure
288
{
289
    /**
290
     * @param string|int|float $number
291
     * @return string
292
     */
293
    return function ($number) use ($precision, $point, $thousands): string {
294
        return \is_numeric($number)
295
            ? \number_format((float) $number, (int) $precision, $point, $thousands)
296
            : '';
297
    };
298
}
299
300
301
302
/**
303
 * Returns a callable for adding C slashes to a string based on a defined char list.
304
 *
305
 * @param string $charList The Char list to add slashes too.
306
 * @return Closure(string):string
307
 */
308
function addSlashes(string $charList): Closure
309
{
310
    /**
311
     * @param string $string The string to have char, slash escaped.
312
     * @return string
313
     */
314
    return function (string $string) use ($charList): string {
315
        return \addcslashes($string, $charList);
316
    };
317
}
318
319
/**
320
 * Returns a callable for splitting a string by a set amount.
321
 *
322
 * @param non-empty-string $separator The char to split by.
323
 * @param int $limit The number of groups to split into.
324
 * @return Closure(string):array<string> The parts.
325
 */
326
function split(string $separator, int $limit = PHP_INT_MAX): Closure
327
{
328
    /**
329
     * @param string $string The string to be split
330
     * @return array<int, string>
331
     */
332
    return function (string $string) use ($separator, $limit): array {
333
        $chunks = explode($separator, $string, $limit);
334
        return is_array($chunks) ? $chunks : array(); // @phpstan-ignore-line, inconsistent errors with differing php versions.
335
    };
336
}
337
338
/**
339
 * Returns a callable for splitting a string by a set amount.
340
 *
341
 * @param int $length The length to split the string up with.
342
 * @return Closure(string):array<string> The parts.
343
 */
344
function splitByLength(int $length): Closure
345
{
346
    /**
347
     * @param string $string The string to be split
348
     * @return array<int, string>
349
     */
350
    return function (string $string) use ($length): array {
351
        return \str_split($string, max(1, $length)) ?: array(); // @phpstan-ignore-line, inconsistent errors with differing php versions.
0 ignored issues
show
Bug Best Practice introduced by
The expression return str_split($string...1, $length)) ?: array() could return the type true which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
352
    };
353
}
354
355
356
/**
357
 * Returns a callback for splitting a string into chunks.
358
 *
359
 * @param int $length The length of each chunk.
360
 * @param string $end The string to use at the end.
361
 * @return Closure(string):string
362
 */
363
function chunk(int $length, string $end = "\r\n"): Closure
364
{
365
    /**
366
     * @param string $string The string to be chunked
367
     * @return string
368
     */
369
    return function (string $string) use ($length, $end): string {
370
        return \chunk_split($string, max(1, $length), $end);
371
    };
372
}
373
374
/**
375
 * Creates a callable for wrapping a string to a defined value.
376
 *
377
 * @param int $width Max width for each "line"
378
 * @param string $break The string to use to denote the end of line.
379
 * @param bool $cut If set to true, words are cut at $width, else overflow.
380
 * @return Closure(string):string
381
 */
382
function wordWrap(int $width, string $break = "\n", bool $cut = false): Closure
383
{
384
    /**
385
     * @param string $string The string to be wrapped
386
     * @return string
387
     */
388
    return function (string $string) use ($width, $break, $cut): string {
389
        return \wordwrap($string, $width, $break, $cut);
390
    };
391
}
392
393
/**
394
 * Returns a callback for counting the number of occurrences of each char in a string.
395
 *
396
 * @link https://www.php.net/manual/en/function.count-chars.php
397
 * @param int $mode See the PHP docs for details.
398
 * @return Closure(string):(int[]|string)
399
 */
400
function countChars(int $mode = 1): Closure
401
{
402
    // Throw an exception if the mode is not supported.
403
    if (! in_array($mode, array( 0, 1, 2, 3, 4 ), true)) {
404
        throw new \InvalidArgumentException('Invalid mode');
405
    }
406
407
    /**
408
     * @param string $string The string to have its char counted.
409
     * @return int[]|string
410
     */
411
    return function (string $string) use ($mode) {
412
        return \count_chars($string, $mode);
413
    };
414
}
415
416
/**
417
 * Returns a callable that counts the occurrences of a given substring in a string
418
 *
419
 * @param string $needle The substring to find
420
 * @param int $offset Place to start, defaults to 0 (start)
421
 * @param int|null $length Max length after offset to search.
422
 * @return Closure(string):int
423
 */
424
function countSubString(string $needle, int $offset = 0, ?int $length = null): Closure
425
{
426
    /**
427
     * @param string $haystack
428
     * @return int
429
     */
430
    return function (string $haystack) use ($needle, $offset, $length): int {
431
        return $length
432
            ? \substr_count($haystack, $needle, $offset, $length)
433
            : \substr_count($haystack, $needle, $offset);
434
    };
435
}
436
437
/**
438
 * Returns a callable for doing repeated trim.
439
 *
440
 * @param string $mask
441
 * @return Closure(string):string
442
 */
443
function trim(string $mask = "\t\n\r\0\x0B"): Closure
444
{
445
    /**
446
     * @param string $string The string to be trimmed
447
     * @return string
448
     */
449
    return function (string $string) use ($mask): string {
450
        return \trim($string, $mask);
451
    };
452
}
453
454
/**
455
 * Returns a callable for doing repeated ltrim.
456
 *
457
 * @param string $mask
458
 * @return Closure(string):string
459
 */
460
function lTrim(string $mask = "\t\n\r\0\x0B"): Closure
461
{
462
    /**
463
     * @param string $string The string to be trimmed
464
     * @return string
465
     */
466
    return function (string $string) use ($mask): string {
467
        return \ltrim($string, $mask);
468
    };
469
}
470
471
/**
472
 * Returns a callable for doing repeated rtrim.
473
 *
474
 * @param string $mask
475
 * @return Closure(string):string
476
 */
477
function rTrim(string $mask = "\t\n\r\0\x0B"): Closure
478
{
479
    /**
480
     * @param string $string The string to be trimmed
481
     * @return string
482
     */
483
    return function (string $string) use ($mask): string {
484
        return \rtrim($string, $mask);
485
    };
486
}
487
488
/**
489
 * Returns a callable for finding the similarities between 2 string.
490
 *
491
 *
492
 * @param string $base The string to act as the base.
493
 * @param bool $asPc If set to true will return the percentage match, rather than char count.
494
 * @return Closure(string):Number
495
 */
496
function similar(string $base, bool $asPc = false): Closure
497
{
498
    /**
499
     * @param string $comparisonString The string to compare against base.
500
     * @return Number
501
     */
502
    return function (string $comparisonString) use ($base, $asPc) {
503
        $pc       = 0.00;
504
        $matching = similar_text($base, $comparisonString, $pc);
505
        return $asPc ? $pc : $matching;
506
    };
507
}
508
509
510
/**
511
 * Returns a callable for padding out a string.
512
 *
513
 * @param int $length Max length to make string.
514
 * @param string $padContent The value to padd the string with (defaults to ' ')
515
 * @param int $type How to pad, please use these constants. STR_PAD_RIGHT|STR_PAD_LEFT|STR_PAD_BOTH
516
 * @return Closure(string):string
517
 */
518
function pad(int $length, string $padContent = ' ', int $type = STR_PAD_RIGHT): Closure
519
{
520
    /**
521
     * @param string $string The string to pad out.
522
     * @return string
523
     */
524
    return function (string $string) use ($length, $padContent, $type): string {
525
        return \str_pad($string, $length, $padContent, $type);
526
    };
527
}
528
529
/**
530
 * Returns a callable for repeating a string by a defined number of times.
531
 *
532
 * @param int $count Number of times to repeat string.
533
 * @return Closure(string):string
534
 */
535
function repeat(int $count): Closure
536
{
537
    /**
538
     * @param string $string The string to repeat
539
     * @return string
540
     */
541
    return function (string $string) use ($count): string {
542
        return \str_repeat($string, $count);
543
    };
544
}
545
546
/**
547
 * Returns a callback for creating a word counter, with set format and char list.
548
 *
549
 * @param int $format can use WORD_COUNT_NUMBER_OF_WORDS | WORD_COUNT_ARRAY | WORD_COUNT_ASSOCIATIVE_ARRAY
550
 * @param string|null $charList The char list of option values considered words.
551
 * @return Closure(string):(int|string[])
552
 */
553
function wordCount(int $format = WORD_COUNT_NUMBER_OF_WORDS, ?string $charList = null): Closure
554
{
555
    /**
556
     * @param string $string The string to pad out.
557
     * @return int|string[]
558
     */
559
    return function (string $string) use ($format, $charList) {
560
        return $charList
561
            ? (\str_word_count($string, $format, $charList) ?: 0)
562
            : (\str_word_count($string, $format) ?: 0);
563
    };
564
}
565
566
/**
567
 * Creates a function for stripping tags with a defined set of allowed tags.
568
 *
569
 * @param string|null $allowedTags The allowed tags, pass null or leave blank for none.
570
 * @return Closure(string):string
571
 */
572
function stripTags(?string $allowedTags = null): Closure
573
{
574
    /**
575
     * @param string $string The string to strip tags from.
576
     * @return string
577
     */
578
    return function (string $string) use ($allowedTags): string {
579
        return $allowedTags
580
            ? \strip_tags($string, $allowedTags)
581
            : \strip_tags($string);
582
    };
583
}
584
585
/**
586
 * Returns a callable for finding the first postition of a defined value in any string.
587
 *
588
 * @param string $needle The value to look for.
589
 * @param int  $offset The offset to start
590
 * @param int $flags STRINGS_CASE_SENSITIVE | STRINGS_CASE_INSENSITIVE
591
 * @return Closure(string):?int
592
 */
593
function firstPosition(string $needle, int $offset = 0, int $flags = STRINGS_CASE_SENSITIVE): Closure
594
{
595
    $caseSensitive = ! (bool) ($flags & STRINGS_CASE_INSENSITIVE); // Assumes true unless INSESNITVE passed
596
597
    /**
598
     * @param string $haystack The haystack to look through.
599
     * @return int|null
600
     */
601
    return function (string $haystack) use ($needle, $offset, $caseSensitive): ?int {
602
        $pos = $caseSensitive
603
            ? strpos($haystack, $needle, $offset)
604
            : stripos($haystack, $needle, $offset);
605
        return is_int($pos) ? (int) $pos : null;
0 ignored issues
show
introduced by
The condition is_int($pos) is always true.
Loading history...
606
    };
607
}
608
609
/**
610
 * Returns a callable for finding the first position of a defined value in any string.
611
 *
612
 * @param string $needle The value to look for.
613
 * @param int  $offset The offset to start
614
 * @param int $flags STRINGS_CASE_SENSITIVE | STRINGS_CASE_INSENSITIVE
615
 * @return Closure(string):?int
616
 */
617
function lastPosition(string $needle, int $offset = 0, int $flags = STRINGS_CASE_SENSITIVE): Closure
618
{
619
    $caseSensitive = ! (bool) ($flags & STRINGS_CASE_INSENSITIVE); // Assumes true unless INSESNITVE passed
620
621
    /**
622
     * @param string $haystack The haystack to look through.
623
     * @return int|null
624
     */
625
    return function (string $haystack) use ($needle, $offset, $caseSensitive): ?int {
626
        $pos = $caseSensitive
627
            ? strrpos($haystack, $needle, $offset)
628
            : strripos($haystack, $needle, $offset);
629
        return is_int($pos) ? (int) $pos : null;
0 ignored issues
show
introduced by
The condition is_int($pos) is always true.
Loading history...
630
    };
631
}
632
633
/**
634
 * Returns a callable for looking for the first occurrence of a substring.
635
 * When found can return all after or before the needle (sub string)
636
 * Can be done as case sensitive (strstr()) or insensitive (stristr())
637
 *
638
 * @param string $needle The substring to look for.
639
 * @param int $flags Possible STRINGS_CASE_INSENSITIVE | STRINGS_CASE_SENSITIVE | STRINGS_AFTER_NEEDLE | STRINGS_BEFORE_NEEDLE
640
 * @return Closure(string):string
641
 */
642
function firstSubString(string $needle, int $flags = STRINGS_CASE_SENSITIVE | STRINGS_AFTER_NEEDLE): Closure
643
{
644
    // Decode flags, only look for none defaults.
645
    $beforeNeedle  = (bool) ($flags & STRINGS_BEFORE_NEEDLE);
646
    $caseSensitive = ! (bool) ($flags & STRINGS_CASE_INSENSITIVE); // Assumes true unless INSESNITVE passed
647
648
    /**
649
     * @param string $haystack The haystack to look through.
650
     * @return string
651
     */
652
    return function (string $haystack) use ($needle, $beforeNeedle, $caseSensitive): string {
653
        $result = $caseSensitive
654
            ? strstr($haystack, $needle, $beforeNeedle)
655
            : stristr($haystack, $needle, $beforeNeedle);
656
        return is_string($result) ? $result : '';
0 ignored issues
show
introduced by
The condition is_string($result) is always true.
Loading history...
657
    };
658
}
659
660
/**
661
 * Returns a callable for creating a function which finds the first occurrence of
662
 * any character (from a list) in a defined string.
663
 *
664
 * @param string $chars All chars to check with.
665
 * @return Closure(string):string
666
 */
667
function firstChar(string $chars): Closure
668
{
669
    /**
670
     * @param string $haystack
671
     * @return string
672
     */
673
    return function (string $haystack) use ($chars): string {
674
        $result = strpbrk($haystack, $chars);
675
        return is_string($result) ? $result : '';
0 ignored issues
show
introduced by
The condition is_string($result) is always true.
Loading history...
676
    };
677
}
678
679
/**
680
 * Returns a function that finds the last char in a string and returns the following text.
681
 * Matches the first char passed, if more than 1 char passed, the rest are ignored.
682
 *
683
 * @param string $char
684
 * @return Closure(string):string
685
 */
686
function lastChar(string $char): Closure
687
{
688
    /**
689
     * @param string $haystack
690
     * @return string
691
     */
692
    return function (string $haystack) use ($char): string {
693
        $result = strrchr($haystack, $char);
694
        return is_string($result) ? $result : '';
0 ignored issues
show
introduced by
The condition is_string($result) is always true.
Loading history...
695
    };
696
}
697
698
/**
699
 * Returns a callable which translates substrings from a defined dictionary.
700
 * Dictionary should be ['from' => 'to' ]
701
 *
702
 * @param array<string, mixed> $dictionary
703
 * @return Closure(string):string
704
 */
705
function translateWith(array $dictionary): Closure
706
{
707
    /**
708
     * @param string $haystack
709
     * @return string
710
     */
711
    return function (string $haystack) use ($dictionary): string {
712
        $result = strtr($haystack, $dictionary);
713
        return $result;
714
    };
715
}
716
717
/**
718
 * Creates a callable for a string safe function compose.
719
 *
720
 * @uses F\composeTypeSafe
721
 * @param callable(mixed):string ...$callable
722
 * @return Closure(mixed):string
723
 */
724
function composeSafeStringFunc(callable ...$callable): Closure
725
{
726
    return F\composeTypeSafe('is_string', ...$callable);
0 ignored issues
show
Bug introduced by
The function composeTypeSafe was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

726
    return /** @scrutinizer ignore-call */ F\composeTypeSafe('is_string', ...$callable);
Loading history...
727
}
728
729
/**
730
 * Creates a callable for compiling a string.
731
 *
732
 * @param string $initial
733
 * @return Closure(string|null):(Closure|string)
734
 */
735
function stringCompiler(string $initial = ''): Closure
736
{
737
    /**
738
     * @param string|null $value
739
     * @return Closure|string
740
     */
741
    return function (?string $value = null) use ($initial) {
742
        if ($value) {
743
            $initial .= $value;
744
        }
745
        return $value ? stringCompiler($initial) : $initial;
746
    };
747
}
748
749
/**
750
 * Checks if the passe value is a string and if its length is 0.
751
 *
752
 * @param mixed $value
753
 * @return bool
754
 */
755
function isBlank($value): bool
756
{
757
    return is_string($value) && \mb_strlen($value) === 0;
758
}
759
760
761
/************************************************************************/
762
/************************************************************************/
763
/**                        Deprecated Functions                        **/
764
/************************************************************************/
765
/************************************************************************/
766
767
768
/**
769
 * See decimalNumber()
770
 * This function will be removed in later versions.
771
 *
772
 * @deprecated Use decimalNumber() instead.
773
 *
774
 * @param int $precision
775
 * @param string $point
776
 * @param string $thousands
777
 * @return Closure
778
 */
779
function decimialNumber($precision = 2, $point = '.', $thousands = ''): Closure
780
{
781
    trigger_error('Deprecated function called. Please use digit. This function will be removed in later versions.', E_USER_DEPRECATED);
782
    return digit($precision, $point, $thousands);
783
}
784
785
/**
786
 * Converts a number (loose type) to a string representation of a float.
787
 *
788
 * @param string|int|float $precision Number of decimal places
789
 * @param string $point The decimal separator
790
 * @param string $thousands The thousand separator.
791
 * @return Closure(string|int|float):string
792
 */
793
function decimalNumber($precision = 2, $point = '.', $thousands = ''): Closure
794
{
795
    trigger_error('Deprecated function called. Please use digit. This function will be removed in later versions.', E_USER_DEPRECATED);
796
    return digit($precision, $point, $thousands);
797
}
798
799
/**
800
 * See similarAsComparison()
801
 *
802
 * @deprecated Use similar() instead.
803
 * @param string $comparisonString
804
 * @param bool $asPc
805
 * @return Closure
806
 */
807
function similarAsComparisson(string $comparisonString, bool $asPc = false): Closure
808
{
809
    trigger_error('Deprecated function called. Please use similarAsComparison. This function will be removed in later versions.', E_USER_DEPRECATED);
810
    return similar($comparisonString, $asPc);
811
}
812
813
814
/**
815
 * Returns a callable for finding the similarities between 2 string.
816
 * This sets the defined value as the base (similar_text as first)
817
 *
818
 * @deprecated Use similar() instead.
819
 * @param string $base The string to act as the base.
820
 * @param bool $asPc If set to true will return the percentage match, rather than char count.
821
 * @return Closure(string):Number
822
 */
823
function similarAsBase(string $base, bool $asPc = false): Closure
824
{
825
    trigger_error('Deprecated function called. Please use similar. This function will be removed in later versions.', E_USER_DEPRECATED);
826
    return similar($base, $asPc);
827
}
828
829
/**
830
 * Returns a callable for finding the similarities between 2 string.
831
 * This sets the defined value as the comparisonString (similar_text as second)
832
 *
833
 * @deprecated Use similar() instead.
834
 * @param string $comparisonString The string to compare against base.
835
 * @param bool $asPc If set to true will return the percentage match, rather than char count.
836
 * @return Closure(string):Number
837
 */
838
function similarAsComparison(string $comparisonString, bool $asPc = false): Closure
839
{
840
    trigger_error('Deprecated function called. Please use similar. This function will be removed in later versions.', E_USER_DEPRECATED);
841
    return similar($comparisonString, $asPc);
842
}
843
844
/**
845
 * See firstPosition()
846
 *
847
 * @deprecated
848
 * @param string $needle
849
 * @param int $offset
850
 * @param int $flags
851
 * @return Closure
852
 */
853
function firstPosistion(string $needle, int $offset = 0, int $flags = STRINGS_CASE_SENSITIVE): Closure
854
{
855
    trigger_error('Deprecated function called. Please use firstPosition. This function will be removed in later versions.', E_USER_DEPRECATED);
856
    return firstPosition($needle, $offset, $flags);
857
}
858
859
/**
860
 * See lastPosition()
861
 *
862
 * @deprecated Use lastPosition() instead.
863
 * @param string $needle The value to look for.
864
 * @param int  $offset The offset to start
865
 * @param int $flags STRINGS_CASE_SENSITIVE | STRINGS_CASE_INSENSITIVE
866
 * @return Closure
867
 */
868
function lastPosistion(string $needle, int $offset = 0, int $flags = STRINGS_CASE_SENSITIVE): Closure
869
{
870
    trigger_error('Deprecated function called. Please use lastPosition. This function will be removed in later versions.', E_USER_DEPRECATED);
871
    return lastPosition($needle, $offset, $flags);
872
}
873