Passed
Push — master ( 54ac54...9db44e )
by Sebastian
05:57
created

IsNumeric::matchForVariable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
/*
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
 * citeproc-php
4
 *
5
 * @link        http://github.com/seboettg/citeproc-php for the source repository
6
 * @copyright   Copyright (c) 2016 Sebastian Böttger.
7
 * @license     https://opensource.org/licenses/MIT
8
 */
9
10
namespace Seboettg\CiteProc\Constraint;
11
12
use NumberFormatter;
13
use Seboettg\CiteProc\CiteProc;
14
use Seboettg\CiteProc\Util\NumberHelper;
15
use stdClass;
16
17
/**
18
 * Class IsNumeric
19
 * @package Seboettg\CiteProc\Choose\Constraint
1 ignored issue
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
20
 *
21
 * @author Sebastian Böttger <[email protected]>
22
 */
23
/** @noinspection PhpUnused */
7 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
24
class IsNumeric extends AbstractConstraint
25
{
26
27
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
28
     * @param string $variable
3 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
29
     * @param stdClass $data
2 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
30
     * @return bool
1 ignored issue
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
31
     */
32 14
    protected function matchForVariable($variable, $data)
33
    {
34 14
        if (isset($data->{$variable})) {
35 4
            return $this->parseValue($data->{$variable});
36
        }
37 11
        return false;
38
    }
39
40
    /**
41
     * Tests whether the given variables (Appendix IV - Variables) contain numeric content. Content is considered
42
     * numeric if it solely consists of numbers. Numbers may have prefixes and suffixes (“D2”, “2b”, “L2d”), and may be
43
     * separated by a comma, hyphen, or ampersand, with or without spaces (“2, 3”, “2-4”, “2 & 4”). For example, “2nd”
44
     * tests “true” whereas “second” and “2nd edition” test “false”.
45
     *
46
     * @param $evalValue
2 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
47
     * @return bool
1 ignored issue
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
48
     */
49 4
    private function parseValue($evalValue)
1 ignored issue
show
Coding Style introduced by
Private method name "IsNumeric::parseValue" must be prefixed with an underscore
Loading history...
50
    {
51 4
        if (is_numeric($evalValue)) {
52 2
            return true;
53 4
        } else if (preg_match(NumberHelper::PATTERN_ORDINAL, $evalValue)) {
54
            $numberFormatter = new NumberFormatter(CiteProc::getContext()->getLocale()->getLanguage(), NumberFormatter::ORDINAL);
55
            return $numberFormatter->parse($evalValue) !== false;
56 4
        } else if (preg_match(NumberHelper::PATTERN_ROMAN, $evalValue)) {
57 1
            return NumberHelper::roman2Dec($evalValue) !== false;
58 4
        } else if (preg_match(NumberHelper::PATTERN_COMMA_AMPERSAND_RANGE, $evalValue)) {
59
            return true;
60
        }
61 4
        return false;
62
    }
63
}
64