Passed
Pull Request — master (#99)
by Alexander
04:32 queued 02:11
created

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Widget;
6
7
use Throwable;
8
use Yiisoft\Definitions\Exception\NotInstantiableException;
9
use Yiisoft\FriendlyException\FriendlyExceptionInterface;
10
11
final class NotInstantiableWithoutWidgetFactoryInitializationException extends NotInstantiableException implements FriendlyExceptionInterface
12
{
13 1
    public function __construct(private ?Throwable $previous)
14
    {
15 1
        parent::__construct(
16 1
            $previous->getMessage() .
0 ignored issues
show
Bug introduced by
The method getMessage() does not exist on null. ( Ignorable by Annotation )

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

16
            $previous->/** @scrutinizer ignore-call */ 
17
                       getMessage() .

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...
17 1
            ' Perhaps need initialize "WidgetFactory" with container for resolve dependencies.',
18 1
            previous: $previous,
19 1
        );
20
    }
21
22 1
    public function getName(): string
23
    {
24 1
        return 'Failed to create a widget. ' . $this->previous->getMessage();
0 ignored issues
show
Bug introduced by
The method getMessage() does not exist on null. ( Ignorable by Annotation )

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

24
        return 'Failed to create a widget. ' . $this->previous->/** @scrutinizer ignore-call */ getMessage();

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...
25
    }
26
27
    public function getSolution(): ?string
28
    {
29
        return <<<'SOLUTION'
30
            Perhaps need initialize `WidgetFactory` with container for resolve dependencies.
31
32
            To initialize the widget factory call `WidgetFactory::initialize()` before using the widget.
33
            It is a good idea to do that for the whole application.
34
35
            Example:
36
37
            ```php
38
            /**
39
             * @var Psr\Container\ContainerInterface $container
40
             */
41
42
            Yiisoft\Widget\WidgetFactory::initialize(
43
                container: $container,
44
                definitions: [MyWidget::class => new MyWidget(/*...*/)],
45
                themes: [
46
                    'custom' => [
47
                        MyWidget::class => [
48
                            'setValue()' => [42],
49
                        ],
50
                    ],
51
                ],
52
                validate: true, // Whether definitions need to be validated.
53
            );
54
            ```
55
56
            See Yii example in the configuration file of this package `config/bootstrap.php`.
57
            SOLUTION;
58
    }
59
}
60