Completed
Push — prototype ( 3bfdd7...aec713 )
by Peter
07:47
created

MySubService::doSomething()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 18 and the first side effect is on line 13.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Service Factory Config Override
4
 * Webino Example
5
 */
6
7
use WebinoAppLib\Event\RouteEvent;
8
use WebinoAppLib\Factory\AbstractFactory;
9
use WebinoAppLib\Feature\Service;
10
use WebinoAppLib\Response\Content\SourcePreview;
11
use WebinoAppLib\Router\DefaultRoute;
12
13
require __DIR__ . '/../../vendor/autoload.php';
14
15
/**
16
 * Example service dependency
17
 */
18
class MySubService
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
19
{
20
    public function doSomething($msg)
21
    {
22
        return $msg;
23
    }
24
}
25
26
/**
27
 * Custom service
28
 */
29
class MyService
0 ignored issues
show
Comprehensibility Best Practice introduced by
The type MyService has been defined more than once; this definition is ignored, only the first definition in examples/simple/public/events-aware/index.php (L50-74) is considered.

This check looks for classes that have been defined more than once.

If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.

This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
30
{
31
    /**
32
     * @var MySubService
33
     */
34
    private $someDependency;
35
36
    public function __construct(MySubService $someDependency)
37
    {
38
        $this->someDependency = $someDependency;
0 ignored issues
show
Bug introduced by
The property someDependency does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
39
    }
40
41
    public function doSomething()
42
    {
43
        return $this->someDependency->doSomething('My service response!');
44
    }
45
}
46
47
/**
48
 * Custom service factory
49
 */
50
class MyServiceFactory extends AbstractFactory
0 ignored issues
show
Comprehensibility Best Practice introduced by
The type MyServiceFactory has been defined more than once; this definition is ignored, only the first definition in examples/simple/public/filesystem-aware/index.php (L35-49) is considered.

This check looks for classes that have been defined more than once.

If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.

This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
51
{
52
    protected function create()
53
    {
54
        /** @var MySubService $someDependency */
55
        $someDependency = $this->getServices()->get(MySubService::class);
56
        return new MyService($someDependency);
0 ignored issues
show
Unused Code introduced by
The call to MyService::__construct() has too many arguments starting with $someDependency.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
57
    }
58
}
59
60
$config = Webino::config([
61
    /**
62
     * Registering service
63
     * factory via config.
64
     */
65
    new Service(MyService::class, stdClass::class),
66
    /**
67
     * Overriding service
68
     * factory via config.
69
     */
70
    new Service(MyService::class, MyServiceFactory::class),
71
72
    // registering example dependency
73
    new Service(MySubService::class),
74
]);
75
76
$app = Webino::application($config)->bootstrap();
77
78
$app->bind(DefaultRoute::class, function (RouteEvent $event) {
79
    /**
80
     * Obtaining service
81
     * instance.
82
     */
83
    $myService = $event->getApp()->get(MyService::class);
84
85
    $event->setResponse([
86
        $myService->doSomething(),
87
        new SourcePreview(__FILE__),
88
    ]);
89
});
90
91
$app->dispatch();
92