Test Failed
Pull Request — master (#110)
by Dmitriy
02:54
created

TableExistsHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 9
c 1
b 0
f 0
dl 0
loc 24
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 16 3
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Gii\Validator;
6
7
use Yiisoft\Db\Connection\ConnectionInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Connection\ConnectionInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Yiisoft\Validator\Exception\UnexpectedRuleException;
9
use Yiisoft\Validator\Result;
10
use Yiisoft\Validator\RuleHandlerInterface;
11
use Yiisoft\Validator\ValidationContext;
12
13
/**
14
 * An inline validator that checks if the attribute value refers to an existing class name.
15
 */
16
final class TableExistsHandler implements RuleHandlerInterface
17
{
18
    public function __construct(
19
        private readonly ConnectionInterface $connection
20
    )
21
    {
22
    }
23
24
    public function validate(mixed $value, object $rule, ValidationContext $context): Result
25
    {
26
        if (!$rule instanceof TableExistsRule) {
27
            throw new UnexpectedRuleException(TableExistsRule::class, $rule);
28
        }
29
30
        $result = new Result();
31
32
        $tableSchema = $this->connection->getTableSchema($value);
33
34
        if ($tableSchema === null) {
35
            $result->addError(sprintf('Table "%s" does not exist.', $value));
36
            return $result;
37
        }
38
39
        return $result;
40
    }
41
}
42