Completed
Pull Request — master (#647)
by Robbie
02:13
created

SubmittedFileField::getLink()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 3
nop 0
1
<?php
2
3
namespace SilverStripe\UserForms\Model\Submission;
4
5
use SilverStripe\Assets\File;
6
use SilverStripe\ORM\FieldType\DBField;
7
8
/**
9
 * A file uploaded on a {@link UserDefinedForm} and attached to a single
10
 * {@link SubmittedForm}.
11
 *
12
 * @package userforms
13
 */
14
15
class SubmittedFileField extends SubmittedFormField
16
{
17
    private static $has_one = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $has_one is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
18
        'UploadedFile' => File::class
19
    ];
20
21
    private static $table_name = 'SubmittedFileField';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $table_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
22
23
    /**
24
     * Return the value of this field for inclusion into things such as
25
     * reports.
26
     *
27
     * @return string
28
     */
29
    public function getFormattedValue()
30
    {
31
        $name = $this->getFileName();
32
        $link = $this->getLink();
33
        $title = _t(__CLASS__.'.DOWNLOADFILE', 'Download File');
34
35
        if ($link) {
36
            return DBField::create_field('HTMLText', sprintf(
37
                '%s - <a href="%s" target="_blank">%s</a>',
38
                $name,
39
                $link,
40
                $title
41
            ));
42
        }
43
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 SilverStripe\UserForms\M...ield::getFormattedValue of type 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
     * Return the value for this field in the CSV export.
49
     *
50
     * @return string
51
     */
52
    public function getExportValue()
53
    {
54
        return ($link = $this->getLink()) ? $link : '';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return ($link = $this->getLink()) ? $link : ''; (string) is incompatible with the return type of the parent method SilverStripe\UserForms\M...rmField::getExportValue of type SilverStripe\UserForms\Model\Submission\Text.

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...
55
    }
56
57
    /**
58
     * Return the link for the file attached to this submitted form field.
59
     *
60
     * @return string
61
     */
62
    public function getLink()
63
    {
64
        if ($file = $this->UploadedFile()) {
0 ignored issues
show
Documentation Bug introduced by
The method UploadedFile does not exist on object<SilverStripe\User...ion\SubmittedFileField>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
65
            if (trim($file->getFilename(), '/') != trim(ASSETS_DIR, '/')) {
66
                return $this->UploadedFile()->AbsoluteLink();
0 ignored issues
show
Documentation Bug introduced by
The method UploadedFile does not exist on object<SilverStripe\User...ion\SubmittedFileField>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
67
            }
68
        }
69
    }
70
71
    /**
72
     * Return the name of the file, if present
73
     *
74
     * @return string
75
     */
76
    public function getFileName()
77
    {
78
        if ($this->UploadedFile()) {
0 ignored issues
show
Documentation Bug introduced by
The method UploadedFile does not exist on object<SilverStripe\User...ion\SubmittedFileField>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
79
            return $this->UploadedFile()->Name;
0 ignored issues
show
Documentation Bug introduced by
The method UploadedFile does not exist on object<SilverStripe\User...ion\SubmittedFileField>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
80
        }
81
    }
82
}
83