GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

AWRequiredFields   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 0
loc 101
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B php() 0 30 7
A getExtendedValidationRoutines() 0 30 5
A setData() 0 4 1
A getData() 0 4 1
A setCaller() 0 4 1
A getCaller() 0 4 1
1
<?php
2
3
namespace Symbiote\AdvancedWorkflow\Forms;
4
5
use SilverStripe\Forms\RequiredFields;
6
7
/**
8
 * Extends RequiredFields so we can prevent DO writes in AW's controller(s) without needing to catch Exceptions
9
 * from DO->validate() all over the place.
10
 * Note specifically $this->getExtendedValidationRoutines() - anti-pattern anyone?
11
 *
12
 * @author Russell Michell [email protected]
13
 * @package advancedworkflow
14
 */
15
class AWRequiredFields extends RequiredFields
16
{
17
    protected $data = array();
18
    protected static $caller;
19
20
    public function php($data)
21
    {
22
        $valid = parent::php($data);
23
        $this->setData($data);
24
25
        // Fetch any extended validation routines on the caller
26
        $extended = $this->getExtendedValidationRoutines();
27
28
        // Only deal-to extended routines once the parent is done
29
        if ($valid && $extended['fieldValid'] !== true) {
30
            $fieldName = $extended['fieldName'];
31
            $formField = $extended['fieldField'];
32
            $errorMessage = sprintf(
33
                $extended['fieldMsg'],
34
                strip_tags('"'.(($formField && $formField->Title()) ? $formField->Title() : $fieldName).'"')
35
            );
36
37
            if ($formField && $msg = $formField->getCustomValidationMessage()) {
38
                $errorMessage = $msg;
39
            }
40
41
            $this->validationError(
42
                $fieldName,
43
                $errorMessage,
44
                "required"
45
            );
46
            $valid = false;
47
        }
48
        return $valid;
49
    }
50
51
    /**
52
     * Allows for the addition of an arbitrary no. additional, dedicated and "extended" validation methods on classes
53
     * that call AWRequiredFields.
54
     * To add specific validation methods to a caller:
55
     *
56
     * 1). Write each checking method using this naming prototype: public function extendedRequiredFieldsXXX(). All
57
     *     methods so named will be called.
58
     * 2). Call AWRequiredFields->setCaller($this)
59
     *
60
     * Each extended method thus called, should return an array of a specific format. (See: static
61
     * $extendedMethodReturn on the caller)
62
     *
63
     * @return array $return
64
     */
65
    public function getExtendedValidationRoutines()
66
    {
67
        // Setup a return array
68
        $return = array(
69
            'fieldValid' => true,
70
            'fieldName'  => null,
71
            'fieldField' => null,
72
            'fieldMsg'   => null,
73
        );
74
        $caller = $this->getCaller();
75
        $methods = get_class_methods($caller);
76
        if (!$methods) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $methods of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
77
            return $return;
78
        }
79
        foreach ($methods as $method) {
80
            if (!preg_match("#extendedRequiredFields#", $method)) {
81
                continue;
82
            }
83
            // One of the DO's validation methods has failed
84
            $extended = $caller->$method($this->getData());
85
            if ($extended['fieldValid'] !== true) {
86
                $return['fieldValid']   = $extended['fieldValid'];
87
                $return['fieldName']    = $extended['fieldName'];
88
                $return['fieldField']   = $extended['fieldField'];
89
                $return['fieldMsg']     = $extended['fieldMsg'];
90
                break;
91
            }
92
        }
93
        return $return;
94
    }
95
96
    protected function setData($data)
97
    {
98
        $this->data = $data;
99
    }
100
101
    protected function getData()
102
    {
103
        return $this->data;
104
    }
105
106
    public function setCaller($caller)
107
    {
108
        self::$caller = $caller;
109
    }
110
111
    public function getCaller()
112
    {
113
        return self::$caller;
114
    }
115
}
116