Failed Conditions
Pull Request — master (#13)
by Adrien
05:33 queued 02:11
created

_tr()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 16
ccs 8
cts 8
cp 1
crap 3
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
use Doctrine\ORM\EntityManager;
6
use Ecodev\Felix\Debug;
7
use Ecodev\Felix\I18n\Translator;
8
use GraphQL\Doctrine\Types;
9
use Laminas\Log\LoggerInterface;
10
11
/**
12
 * Returns the type registry.
13
 */
14
function _types(): Types
15
{
16
    global $container;
17
18
    return $container->get(Types::class);
19
}
20
21
/**
22
 * Returns the Entity Manager.
23
 */
24
function _em(): EntityManager
25
{
26
    global $container;
27
28 7
    return $container->get(EntityManager::class);
29
}
30
31
/**
32
 * Returns logger.
33
 */
34
function _log(): LoggerInterface
35
{
36
    global $container;
37
38
    return $container->get(LoggerInterface::class);
39
}
40
41
/**
42
 * Export variables omitting array keys that are strictly numeric.
43
 *
44
 * By default will output result
45
 *
46
 * @param mixed $data
47
 *
48
 * @return string string representation of variable
49
 */
50
function ve($data, bool $return = false): string
51
{
52
    return Debug::export($data, $return);
53
}
54
55
/**
56
 * Dump all arguments.
57
 */
58
function v(): void
59
{
60
    var_dump(func_get_args());
0 ignored issues
show
Security Debugging Code introduced by
var_dump(func_get_args()) looks like debug code. Are you sure you do not want to remove it?
Loading history...
61
}
62
63
/**
64
 * Dump all arguments and die.
65
 */
66
function w(): never
1 ignored issue
show
Bug introduced by
The type never 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...
67
{
68
    $isHtml = (PHP_SAPI !== 'cli');
69
    echo "\n_________________________________________________________________________________________________________________________" . ($isHtml ? '</br>' : '') . "\n";
70
    var_dump(func_get_args());
0 ignored issues
show
Security Debugging Code introduced by
var_dump(func_get_args()) looks like debug code. Are you sure you do not want to remove it?
Loading history...
71
    echo "\n" . ($isHtml ? '</br>' : '') . '_________________________________________________________________________________________________________________________' . ($isHtml ? '<pre>' : '') . "\n";
72
    debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
73
    echo '' . ($isHtml ? '</pre>' : '') . '_________________________________________________________________________________________________________________________' . ($isHtml ? '</br>' : '') . "\n";
74
    exit("script aborted on purpose.\n");
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return never. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
75
}
76
77
/**
78
 * Translate given message in current language.
79
 *
80
 * If replacements are given, they will be replaced after translation:
81
 *
82
 * ```php
83
 * _tr('Hello %my-name%', ['my-name' => 'John']); // Bonjour John
84
 * ```
85
 *
86
 * @param array<string, null|float|int|string> $replacements
87
 */
88
function _tr(string $message, array $replacements = []): string
89
{
90
    global $container;
91
92 10
    $translator = $container->get(Translator::class);
93 10
    $translation = $translator->translate($message);
94 10
    if (!$replacements) {
95 6
        return $translation;
96
    }
97
98 4
    $finalReplacements = [];
99 4
    foreach ($replacements as $key => $value) {
100 4
        $finalReplacements['%' . $key . '%'] = $value;
101
    }
102
103 4
    return strtr($translation, $finalReplacements);
104
}
105