Issues (3)

src/Value/ValueValidator.php (2 issues)

1
<?php
2
/**
3
 * This file is part of the eav package.
4
 *
5
 * @author    Alex Kuperwood <[email protected]>
6
 * @copyright 2025 Alex Kuperwood
7
 * @license   https://opensource.org/license/mit  The MIT License
8
 */
9
declare(strict_types=1);
10
11
namespace Kuperwood\Eav\Value;
12
13
use Kuperwood\Eav\Enum\_RESULT;
14
use Kuperwood\Eav\Enum\_VALUE;
15
use Kuperwood\Eav\Enum\ATTR_TYPE;
16
use Kuperwood\Eav\Traits\ContainerTrait;
17
use Kuperwood\Eav\Validation\Constraints\NotBlankConstraint;
18
use Kuperwood\Eav\Validation\Constraints\NumericConstraint;
19
use Kuperwood\Eav\Validation\Constraints\RequiredConstraint;
20
use Kuperwood\Eav\Validation\Validator;
21
22
class ValueValidator
23
{
24
    use ContainerTrait;
25
26
    public function getDefaultValueRule(): array
27
    {
28
        return ATTR_TYPE::validationRule($this->getAttributeContainer()->getAttribute()->getType());
29 1
    }
30
31 1
    public function getRules(): array
32
    {
33
        $rules = $this->getAttributeContainer()->getStrategy()->rules();
0 ignored issues
show
Are you sure the assignment to $rules is correct as $this->getAttributeConta...>getStrategy()->rules() targeting Kuperwood\Eav\Strategy::rules() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
34 3
        return [
35
            _VALUE::ENTITY_ID => [new RequiredConstraint(), new NotBlankConstraint(), new NumericConstraint()],
36 3
            _VALUE::DOMAIN_ID => [new RequiredConstraint(), new NotBlankConstraint(),  new NumericConstraint()],
37 3
            _VALUE::ATTRIBUTE_ID => [new RequiredConstraint(), new NotBlankConstraint(),  new NumericConstraint()],
38 3
            _VALUE::VALUE => is_null($rules)
0 ignored issues
show
The condition is_null($rules) is always true.
Loading history...
39 3
                ? $this->getDefaultValueRule()
40 3
                : $rules,
41 3
        ];
42 1
    }
43 3
44 3
    public function getValidatedData(): array
45
    {
46
        $container = $this->getAttributeContainer();
47 1
        $attribute = $container->getAttribute();
48
        $entity = $container->getAttributeSet()->getEntity();
49 1
        $valueManager = $container->getValueManager();
50 1
51 1
        return [
52 1
            _VALUE::ENTITY_ID => $entity->getKey(),
53
            _VALUE::DOMAIN_ID => $entity->getDomainKey(),
54 1
            _VALUE::ATTRIBUTE_ID => $attribute->getKey(),
55 1
            _VALUE::VALUE => $valueManager->getRuntime(),
56 1
        ];
57 1
    }
58 1
59 1
    public function getValidator(): Validator
60
    {
61
        return new Validator();
62 1
    }
63
64 1
    public function validateField()
65
    {
66
        $output = null;
67 2
        $container = $this->getAttributeContainer();
68
        $strategy = $container->getStrategy();
69 2
        $result = $strategy->validate();
70 2
        if ($result->getCode() == _RESULT::VALIDATION_FAILS) {
71 2
            $output = $result->getData();
72 2
        }
73 2
74 1
        return $output;
75
    }
76
}
77