Completed
Push — master ( 01ba7c...6d88ca )
by Damian
21:36 queued 11:17
created

FormSchema   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
c 3
b 0
f 0
lcom 0
cbo 1
dl 0
loc 64
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getSchema() 0 28 6
A getState() 0 20 3
1
<?php
2
3
namespace SilverStripe\Forms\Schema;
4
5
use Convert;
6
use Form;
7
8
/**
9
 * Class FormSchema
10
 * @package SilverStripe\Forms\Schema
11
 *
12
 * Represents a {@link Form} as structured data which allows a frontend library to render it.
13
 * Includes information about the form as well as its fields.
14
 * Can create a "schema" (structure only) as well as "state" (data only).
15
 */
16
class FormSchema {
17
18
	/**
19
	 * Gets the schema for this form as a nested array.
20
	 *
21
	 * @param Form $form
22
	 * @return array
23
	 */
24
	public function getSchema(Form $form) {
25
		$request = $form->controller()->getRequest();
0 ignored issues
show
Deprecated Code introduced by
The method Form::Controller() has been deprecated with message: 4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
26
		$params = $request->AllParams();
27
28
		$schema = [
29
			'name' => $form->getName(),
30
			'id' => isset($params['ID']) ? $params['ID'] : null,
31
			'action' => isset($params['Action']) ? $params['Action'] : null,
32
			'method' => $form->controller()->getRequest()->HttpMethod(),
0 ignored issues
show
Deprecated Code introduced by
The method Form::Controller() has been deprecated with message: 4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
33
			'schema_url' => $request->getUrl(),
34
			'attributes' => $form->getAttributes(),
35
			'data' => [],
36
			'fields' => [],
37
			'actions' => []
38
		];
39
40
		foreach ($form->Actions() as $action) {
0 ignored issues
show
Bug introduced by
The expression $form->Actions() of type object<FieldList>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
41
			$schema['actions'][] = $action->getSchemaData();
42
		}
43
44
		foreach ($form->Fields() as $fieldList) {
0 ignored issues
show
Bug introduced by
The expression $form->Fields() of type object<FieldList>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
45
			foreach ($fieldList->getForm()->fields()->dataFields() as $field) {
46
				$schema['fields'][] = $field->getSchemaData();
47
			}
48
		}
49
50
		return $schema;
51
	}
52
53
	/**
54
	 * Gets the current state of this form as a nested array.
55
	 *
56
	 * @param Form $form
57
	 * @return array
58
	 */
59
	public function getState(Form $form) {
60
		$state = [
61
			'id' => $form->getName(),
62
			'fields' => [],
63
			'messages' => []
64
		];
65
66
		foreach ($form->Fields()->dataFields() as $field) {
67
			$state['fields'][] = $field->getSchemaState();
68
		}
69
70
		if($form->Message()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $form->Message() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
71
			$state['messages'][] = [
72
				'value' => $form->Message(),
73
				'type' => $form->MessageType(),
74
			];
75
		}
76
77
		return $state;
78
	}
79
}
80