Passed
Push — master ( 68bace...6d1614 )
by Brian
02:33 queued 12s
created

LaravelXmlServiceProvider::boot()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 63
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5.005

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
c 2
b 0
f 0
dl 0
loc 63
ccs 16
cts 17
cp 0.9412
rs 9.3888
cc 5
nc 2
nop 0
crap 5.005

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Bmatovu\LaravelXml;
4
5
use Bmatovu\LaravelXml\Http\XmlResponse;
6
use Bmatovu\LaravelXml\Support\Facades\LaravelXml;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Bmatovu\LaravelXml\LaravelXml. 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...
7
use Bmatovu\LaravelXml\Support\XmlElement;
8
use Illuminate\Support\Facades\Request;
9
use Illuminate\Support\Facades\Response;
10
use Illuminate\Support\ServiceProvider;
11
use Illuminate\Support\Str;
12
13
class LaravelXmlServiceProvider extends ServiceProvider
14
{
15
    /**
16
     * Bootstrap the application services.
17
     */
18 14
    public function boot(): void
19
    {
20 14
        if ($this->app->runningInConsole()) {
21 14
            $this->publishes([
22 14
                __DIR__.'/../config/xml.php' => config_path('xml.php'),
23
            ], 'config');
24
        }
25
26
        /*
27
         * Determine if the request is sending XML.
28
         * @deprecated v3.0.0
29
         *
30
         * @return bool
31
         */
32 14
        Request::macro('isXml', function () {
33 3
            return 'xml' === $this->getContentType();
0 ignored issues
show
Bug introduced by
The method getContentType() does not exist on Bmatovu\LaravelXml\LaravelXmlServiceProvider. ( Ignorable by Annotation )

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

33
            return 'xml' === $this->/** @scrutinizer ignore-call */ getContentType();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
34
        });
35
36
        /*
37
         * Determine if the request is sending XML.
38
         *
39
         * @return bool
40
         */
41 14
        Request::macro('sentXml', function () {
42
            return 'xml' === $this->getContentType();
43
        });
44
45
        /*
46
         * Get the XML payload for the request.
47
         *
48
         * @return \Bmatovu\LaravelXml\Support\XmlElement
49
         */
50 14
        Request::macro('xml', function () {
51 2
            if (! $this->isXml() || ! $content = $this->getContent()) {
0 ignored issues
show
Bug introduced by
The method isXml() does not exist on Bmatovu\LaravelXml\LaravelXmlServiceProvider. ( Ignorable by Annotation )

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

51
            if (! $this->/** @scrutinizer ignore-call */ isXml() || ! $content = $this->getContent()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getContent() does not exist on Bmatovu\LaravelXml\LaravelXmlServiceProvider. ( Ignorable by Annotation )

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

51
            if (! $this->isXml() || ! $content = $this->/** @scrutinizer ignore-call */ getContent()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
52 1
                return new XmlElement('<document></document>');
53
            }
54
55 1
            return simplexml_load_string($content, XmlElement::class);
56
        });
57
58
        /*
59
         * Determine if the current request is asking for XML in return.
60
         *
61
         * @return bool
62
         */
63 14
        Request::macro('wantsXml', function () {
64 1
            $acceptable = $this->getAcceptableContentTypes();
0 ignored issues
show
Bug introduced by
The method getAcceptableContentTypes() does not exist on Bmatovu\LaravelXml\LaravelXmlServiceProvider. ( Ignorable by Annotation )

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

64
            /** @scrutinizer ignore-call */ 
65
            $acceptable = $this->getAcceptableContentTypes();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
66 1
            return isset($acceptable[0]) && Str::contains($acceptable[0], ['/xml', '+xml']);
67
        });
68
69
        /*
70
         * Return a new XML response from the application.
71
         *
72
         * @param  string|array $data
73
         * @param  int $status
74
         * @param  array $headers
75
         * @param  array $options
76
         *
77
         * @return \Bmatovu\LaravelXml\Http\XmlResponse
78
         */
79 14
        Response::macro('xml', function ($data, $status = 200, array $headers = [], $options = []) {
80 2
            return new XmlResponse($data, $status, $headers, $options);
81
        });
82
    }
83
84
    /**
85
     * Register the application services.
86
     */
87 14
    public function register(): void
88
    {
89 14
        $this->mergeConfigFrom(__DIR__.'/../config/xml.php', 'xml');
90
91 14
        $this->app->bind('laravel-xml', function () {
92 7
            return new LaravelXml();
93
        });
94
    }
95
}
96