Completed
Push — webpack ( 4abc02...0465cf )
by Sam
07:36
created

InlineFormAction_ReadOnly   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 23
rs 10
wmc 2
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A Field() 0 10 1
A Title() 0 3 1
1
<?php
2
3
/**
4
 * Render a button that will submit the form its contained in through ajax.
5
 *
6
 * @see framework/client/dist/js/InlineFormAction.js
7
 *
8
 * @package forms
9
 * @subpackage actions
10
 */
11
class InlineFormAction extends FormField {
12
13
	/**
14
	 * Create a new action button.
15
	 *
16
	 * @param string $action The method to call when the button is clicked
17
	 * @param string $title The label on the button
18
	 * @param string $extraClass A CSS class to apply to the button in addition to 'action'
19
	 */
20
	public function __construct($action, $title = "", $extraClass = '') {
21
		$this->extraClass = ' '.$extraClass;
22
		parent::__construct($action, $title);
23
	}
24
25
	public function performReadonlyTransformation() {
26
		return $this->castedCopy('InlineFormAction_ReadOnly');
27
	}
28
29
	/**
30
	 * @param array $properties
31
	 * @return string
32
	 */
33
	public function Field($properties = array()) {
34
		return FormField::create_tag('input', array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \FormField::creat..., $this->extraClass))); (string) is incompatible with the return type of the parent method FormField::Field of type SilverStripe\ORM\FieldType\DBHTMLText.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
35
				'type' => 'submit',
36
				'name' => sprintf('action_%s', $this->getName()),
37
		        'value' => $this->title,
38
		        'id' => $this->ID(),
39
		        'class' => sprintf('action%s', $this->extraClass),
40
		));
41
	}
42
43
	public function Title() {
44
		return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type of the parent method FormField::Title of type null|string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
45
	}
46
}
47
48
/**
49
 * Readonly version of {@link InlineFormAction}.
50
 * @package forms
51
 * @subpackage actions
52
 */
53
class InlineFormAction_ReadOnly extends FormField {
54
55
	protected $readonly = true;
56
57
	/**
58
	 * @param array $properties
59
	 * @return string
60
	 */
61
	public function Field($properties = array()) {
62
		return FormField::create_tag('input', array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \FormField::creat... . $this->extraClass)); (string) is incompatible with the return type of the parent method FormField::Field of type SilverStripe\ORM\FieldType\DBHTMLText.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
63
				'type' => 'submit',
64
	            'name' => sprintf('action_%s', $this->name),
65
	            'value' => $this->title,
66
			'id' => $this->ID(),
67
				'disabled' => 'disabled',
68
	            'class' => 'action disabled ' . $this->extraClass,
69
		));
70
	}
71
72
	public function Title() {
73
		return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type of the parent method FormField::Title of type null|string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
74
	}
75
}
76