Completed
Pull Request — master (#3)
by ARCANEDEV
03:17
created

FoundationServiceProvider::publishAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php namespace Arcanesoft\Foundation;
2
3
use Arcanesoft\Core\Bases\PackageServiceProvider;
4
5
/**
6
 * Class     FoundationServiceProvider
7
 *
8
 * @package  Arcanesoft\Foundation
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class FoundationServiceProvider extends PackageServiceProvider
12
{
13
    /* ------------------------------------------------------------------------------------------------
14
     |  Properties
15
     | ------------------------------------------------------------------------------------------------
16
     */
17
    /**
18
     * Package name.
19
     *
20
     * @var string
21
     */
22
    protected $package = 'foundation';
23
24
    /* ------------------------------------------------------------------------------------------------
25
     |  Getters & Setters
26
     | ------------------------------------------------------------------------------------------------
27
     */
28
    /**
29
     * Get the base path of the package.
30
     *
31
     * @return string
32
     */
33 24
    public function getBasePath()
34
    {
35 24
        return dirname(__DIR__);
36
    }
37
38
    /* ------------------------------------------------------------------------------------------------
39
     |  Main Functions
40
     | ------------------------------------------------------------------------------------------------
41
     */
42
    /**
43
     * Register the service provider.
44
     */
45 24
    public function register()
46
    {
47 24
        $this->registerConfig();
48 24
        $this->registerSidebarItems();
49 24
        $this->registerServiceProviders();
50 24
        $this->registerFoundationService();
51
52 24
        if ($this->app->runningInConsole()) {
53 24
            $this->app->register(Providers\CommandServiceProvider::class);
54 18
        }
55 24
    }
56
57
    /**
58
     * Boot the service provider.
59
     */
60 24
    public function boot()
61
    {
62 24
        $this->publishAll();
63
64 24
        $this->app->register(Providers\RouteServiceProvider::class);
65 24
        $this->app->register(Providers\ComposerServiceProvider::class);
66 24
    }
67
68
    /**
69
     * Get the services provided by the provider.
70
     *
71
     * @return array
72
     */
73 4
    public function provides()
74
    {
75
        return ['arcanesoft.foundation'];
76 4
    }
77 3
78
    /* ------------------------------------------------------------------------------------------------
79
     |  Services Functions
80
     | ------------------------------------------------------------------------------------------------
81
     */
82
    /**
83
     * Register all the required service providers.
84
     */
85
    private function registerServiceProviders()
86
    {
87 24
        $this->app->register(\Arcanesoft\Core\CoreServiceProvider::class);
88
        $this->app->register(Providers\PackagesServiceProvider::class);
89 24
        $this->app->register(Providers\ModuleServiceProvider::class);
90 24
        $this->app->register(Providers\AuthorizationServiceProvider::class);
91 24
    }
92 24
93 24
    /**
94
     * Register Foundation service.
95
     */
96
    private function registerFoundationService()
97
    {
98 24
        $this->singleton('arcanesoft.foundation', Foundation::class);
99
    }
100 24
101 24
    /* ------------------------------------------------------------------------------------------------
102
     |  Package Functions
103
     | ------------------------------------------------------------------------------------------------
104
     */
105
    /**
106
     * Publish all the package files.
107
     *
108
     * @param  bool  $load
109
     */
110 24
    protected function publishAll($load = true)
111
    {
112 24
        parent::publishAll($load);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Arcanesoft\Core\Bases\PackageServiceProvider as the method publishAll() does only exist in the following sub-classes of Arcanesoft\Core\Bases\PackageServiceProvider: Arcanesoft\Foundation\FoundationServiceProvider. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
113
114
        $this->publishes([
115 24
            $this->getBasePath() . '/resources/assets/dist' => public_path("vendor/{$this->package}"),
116 24
        ], 'assets');
117 24
    }
118
}
119