Completed
Branch dev (1e869a)
by
unknown
05:31
created

AdminPageFramework_Form_Base::callBack()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 2
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A AdminPageFramework_Form_Base::__toString() 0 3 1
1
<?php
2
/**
3
 * Admin Page Framework
4
 * 
5
 * http://en.michaeluno.jp/admin-page-framework/
6
 * Copyright (c) 2013-2016 Michael Uno; Licensed MIT
7
 * 
8
 */
9
10
/**
11
 * Provides shared methods for the form class.
12
 * 
13
 * @package     AdminPageFramework
14
 * @subpackage  Common/Form
15
 * @since       3.7.0
16
 * @internal
17
 */
18
abstract class AdminPageFramework_Form_Base extends AdminPageFramework_Form_Utility {
19
    
20
    /**
21
     * Stores resource items. 
22
     * 
23
     * @internal
24
     */
25
    static public $_aResources = array(
26
        'internal_styles'    => array(),
27
        'internal_styles_ie' => array(),
28
        'internal_scripts'   => array(),
29
        'src_styles'         => array(),
30
        'src_scripts'        => array(),
31
    );    
32
    
33
    /**
34
     * Checks if a given array holds fieldsets or not.
35
     * 
36
     * @todo        It seems this method is not used. If so deprecate it.
37
     * @return      boolean
38
     */
39
    // public function isFieldsets( array $aItems ) {
40
        // $_aItem = $this->getFirstElement( $aItems );
41
        // return isset( $_aItem[ 'field_id' ], $_aItem[ 'section_id' ] );
42
    // }
43
    
44
    /**
45
     * Determines whether the given ID is of a registered form section.
46
     * 
47
     * Consider the possibility that the given ID may be used both for a section and a field.
48
     * 
49
     * 1. Check if the given ID is not a section.
50
     * 2. Parse stored fields and check their ID. If one matches, return false.
51
     * 
52
     * @since       3.0.0
53
     * @since       3.7.0      Moved from `AdminPageFramework_FormDefinition_Base`.
54
     */
55
    public function isSection( $sID ) {
56
// @todo Find a way for nested sections.        
57
        // Integer IDs are not accepted as they are reserved for sub-sections.
58
        if ( $this->isNumericInteger( $sID ) ) {
59
            return false;
60
        }
61
        
62
        // If the section ID is not registered, return false.
63
        if ( ! array_key_exists( $sID, $this->aSectionsets ) ) { 
0 ignored issues
show
Bug introduced by
The property aSectionsets does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
64
            return false; 
65
        }
66
        
67
        // the fields array's first dimension is also filled with the keys of section ids.
68
        if ( ! array_key_exists( $sID, $this->aFieldsets ) ) { 
0 ignored issues
show
Bug introduced by
The property aFieldsets does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
69
            return false; 
70
        }
71
        
72
        // Since numeric IDs are denied at the beginning of the method, the elements will not be sub-sections.
73
        $_bIsSeciton = false;
74
        foreach( $this->aFieldsets as $_sSectionID => $_aFields ) {    
75
        
76
            if ( $_sSectionID == $sID ) { 
77
                $_bIsSeciton = true; 
78
            }
79
            
80
            // a field using the ID is found, and it precedes a section match.     
81
            if ( array_key_exists( $sID, $_aFields ) ) { 
82
                return false; 
83
            }
84
            
85
        }
86
        
87
        return $_bIsSeciton;
88
        
89
    }        
90
    
91
    /**
92
     * Decides whether the current user including guests can view the form or not.
93
     * 
94
     * To allow guests to view the form set an empty value to it.
95
     * 
96
     * @since       3.7.0
97
     * @return      boolean
98
     */
99
    public function canUserView( $sCapability ) {
100
        
101
        if ( ! $sCapability  ) {
102
            return true;
103
        }
104
        
105
        return ( boolean ) current_user_can( $sCapability );
106
        
107
    }
108
109
    /**
110
     * Decides whether the form elements should be registered or not.
111
     * 
112
     * @access      public      A delegation class accesses this method so it must be public.
113
     * @since       3.7.0
114
     * @return      boolean
115
     */
116
    public function isInThePage() {
117
        return $this->callBack(
118
            $this->aCallbacks[ 'is_in_the_page' ], 
0 ignored issues
show
Bug introduced by
The property aCallbacks does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
119
            true
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
120
        );
121
    }    
122
    
123
    /**
124
     * Prevents the output from getting too long when the object is dumped.
125
     *
126
     * Field definition arrays contain the factory object reference and when the debug log method tries to dump it, the output gets too long.
127
     * So shorten it here.
128
     * 
129
     * @remark      Called when the object is called as a string.
130
     * @since       3.7.0
131
     */   
132
    public function __toString() {
133
        return $this->getObjectInfo( $this );        
134
    }
135
    
136
}
137