alphanumeric()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
use GuzzleHttp\HandlerStack;
4
use GuzzleHttp\MessageFormatter;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, MessageFormatter. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
5
use GuzzleHttp\Middleware;
6
use Illuminate\Container\Container;
7
use Illuminate\Support\Str;
8
9
if (! function_exists('environment_file_path')) {
10
    /**
11
     * Get application environment file path.
12
     *
13
     * For both Laravel and Lumen framework.
14
     *
15
     * @param string  $helper
16
     * @param string  $envFile
17
     *
18
     * @return string
19
     */
20
    function environment_file_path($helper = 'environmentFilePath', $envFile = '.env'): string
21
    {
22 12
        $app = Container::getInstance();
23
24 12
        if (method_exists($app, $helper)) {
25 12
            return $app->{$helper}();
26
        }
27
28
        // Lumen
29 1
        return $app->basePath($envFile);
0 ignored issues
show
introduced by
The method basePath() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

29
        return $app->/** @scrutinizer ignore-call */ basePath($envFile);
Loading history...
30
    }
31
}
32
33
if (! function_exists('alphanumeric')) {
34
    /**
35
     * Strip all symbols from a string.
36
     *
37
     * @see https://stackoverflow.com/a/16791863/2732184 Source
38
     *
39
     * @param string  $str
40
     *
41
     * @return string
42
     */
43
    function alphanumeric($str): string
44
    {
45 7
        return preg_replace('/[^\p{L}\p{N}\s]/u', '', $str);
46
    }
47
}
48
49
if (! function_exists('array_merge_recursive_distinct')) {
50
    /**
51
     * Merge arrays recursively - but distinctly.
52
     *
53
     * @see https://www.php.net/manual/en/function.array-merge-recursive.php#92195 Issue
54
     * @see https://stackoverflow.com/a/25712428 Solution
55
     *
56
     * @param string  $str
57
     *
58
     * @return string
59
     */
60
    function array_merge_recursive_distinct(array $array1, array $array2)
61
    {
62
        $merged = $array1;
63
64
        foreach ($array2 as $key => & $value) {
65
            if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
66
                $merged[$key] = array_merge_recursive_distinct($merged[$key], $value);
67
            } elseif (is_numeric($key)) {
68
                if (! in_array($value, $merged)) {
69
                    $merged[] = $value;
70
                }
71
            } else {
72
                $merged[$key] = $value;
73
            }
74
        }
75
76
        return $merged;
77
    }
78
}
79
80
if (! function_exists('append_log_middleware')) {
81
    /**
82
     * Add log middleware to handler stack.
83
     *
84
     * @see https://github.com/guzzle/guzzle/blob/master/src/MessageFormatter.php
85
     *
86
     * @param \GuzzleHttp\HandlerStack $handlerStack
87
     *
88
     * @throws \Exception
89
     *
90
     * @return \GuzzleHttp\HandlerStack
91
     */
92
    function append_log_middleware(HandlerStack $handlerStack)
93
    {
94 28
        $id = Str::random(10);
95
96 28
        $messageFormats = [
97 28
            "HTTP_OUT_{$id} [Request] {method} {target}" => 'info',
98 28
            "HTTP_OUT_{$id} [Request] [Headers] \n{req_headers}" => 'debug',
99 28
            "HTTP_OUT_{$id} [Request] [Body] {req_body}" => 'debug',
100 28
            "HTTP_OUT_{$id} [Response] HTTP/{version} {code} {phrase} Size: {res_header_Content-Length}" => 'info',
101 28
            "HTTP_OUT_{$id} [Response] [Headers] \n{res_headers}" => 'debug',
102 28
            "HTTP_OUT_{$id} [Response] [Body] {res_body}" => 'debug',
103 28
            "HTTP_OUT_{$id} [Error] {error}" => 'error',
104 28
        ];
105
106 28
        $logger = Container::getInstance()->get('log');
107
108 28
        collect($messageFormats)->each(function ($level, $format) use ($logger, $handlerStack) {
0 ignored issues
show
Bug introduced by
$messageFormats of type array<string,string> is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

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

108
        collect(/** @scrutinizer ignore-type */ $messageFormats)->each(function ($level, $format) use ($logger, $handlerStack) {
Loading history...
109 28
            $messageFormatter = new MessageFormatter($format);
110
111 28
            $logMiddleware = Middleware::log($logger, $messageFormatter, $level);
112
113 28
            $handlerStack->unshift($logMiddleware);
114 28
        });
115
116 28
        return $handlerStack;
117
    }
118
}
119