Completed
Push — develop ( c1ef57...2d3e63 )
by Neomerx
36:35 queued 15:21
created

Validation::getMessages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php declare (strict_types = 1);
2
3
namespace Limoncello\Flute\L10n;
4
5
/**
6
 * Copyright 2015-2019 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Limoncello\Flute\Contracts\Validation\ErrorCodes;
22
use Limoncello\Validation\I18n\EnUsLocale;
23
24
/**
25
 * @package Limoncello\Flute
26
 */
27
class Validation
28
{
29
    /** @var string Namespace name for message keys. */
30
    const NAMESPACE_NAME = 'Limoncello.Flute.Validation';
31
32
    /**
33
     * @var array
34
     */
35
    private static $messages = EnUsLocale::MESSAGES + [
36
        ErrorCodes::TYPE_MISSING                => 'JSON API type should be specified.',
37
        ErrorCodes::INVALID_ATTRIBUTES          => 'JSON API attributes are invalid.',
38
        ErrorCodes::UNKNOWN_ATTRIBUTE           => 'Unknown JSON API attribute.',
39
        ErrorCodes::INVALID_RELATIONSHIP_TYPE   => 'The value should be a valid JSON API relationship type.',
40
        ErrorCodes::INVALID_RELATIONSHIP        => 'Invalid JSON API relationship.',
41
        ErrorCodes::UNKNOWN_RELATIONSHIP        => 'Unknown JSON API relationship.',
42
        ErrorCodes::EXIST_IN_DATABASE_SINGLE    => 'The value should be a valid identifier.',
43
        ErrorCodes::EXIST_IN_DATABASE_MULTIPLE  => 'The value should be valid identifiers.',
44
        ErrorCodes::UNIQUE_IN_DATABASE_SINGLE   => 'The value should be a unique identifier.',
45
        ErrorCodes::INVALID_OPERATION_ARGUMENTS => 'Invalid Operation Arguments.',
46
    ];
47
48
    /**
49
     * @param int $errorCode
50
     *
51
     * @return string
52
     */
53 21
    public static function convertCodeToDefaultMessage(int $errorCode): string
54
    {
55 21
        return static::getMessages()[$errorCode];
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61 21
    protected static function getMessages(): array
62
    {
63 21
        return static::$messages;
0 ignored issues
show
Bug introduced by
Since $messages is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $messages to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
64
    }
65
}
66