Completed
Push — master ( 054463...ec8550 )
by Richard
11s
created

SystemFineAvatarUploadHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 7.14 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 3
loc 42
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B storeUploadedFile() 3 28 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * SystemFineImUploadHandler class to work with ajaxfineupload.php endpoint
5
 * to facilitate uploads for the system image manager
6
 *
7
 * Do not use or reference this directly from your client-side code.
8
 * Instead, this should be required via the endpoint.php or endpoint-cors.php
9
 * file(s).
10
 *
11
 * @license   MIT License (MIT)
12
 * @copyright Copyright (c) 2015-present, Widen Enterprises, Inc.
13
 * @link      https://github.com/FineUploader/php-traditional-server
14
 *
15
 * The MIT License (MIT)
16
 *
17
 * Copyright (c) 2015-present, Widen Enterprises, Inc.
18
 *
19
 * Permission is hereby granted, free of charge, to any person obtaining a copy
20
 * of this software and associated documentation files (the "Software"), to deal
21
 * in the Software without restriction, including without limitation the rights
22
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
23
 * copies of the Software, and to permit persons to whom the Software is
24
 * furnished to do so, subject to the following conditions:
25
 *
26
 * The above copyright notice and this permission notice shall be included in all
27
 * copies or substantial portions of the Software.
28
 *
29
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
34
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35
 * SOFTWARE.
36
 */
37
38
class SystemFineAvatarUploadHandler extends SystemFineUploadHandler
39
{
40
    /**
41
     * XoopsFineAvatarUploadHandler constructor.
42
     * @param stdClass $claims claims passed in JWT header
43
     */
44
    public function __construct(\stdClass $claims)
45
    {
46
        parent::__construct($claims);
47
        $this->allowedMimeTypes = array('image/gif', 'image/jpeg', 'image/png');
48
        $this->allowedExtensions = array('gif', 'jpeg', 'jpg', 'png');
49
    }
50
51
    protected function storeUploadedFile($target, $mimeType, $uuid)
52
    {
53
        $pathParts = pathinfo($this->getName());
54
        $avatarName = uniqid('savt') . '.' . strtolower($pathParts['extension']);
55
        $avatarNicename = str_replace(array('_','-'), ' ', $pathParts['filename']);
56
        $avatarPath = XOOPS_ROOT_PATH . '/uploads/avatars/' . $avatarName;
57
58 View Code Duplication
        if (false === move_uploaded_file($_FILES[$this->inputName]['tmp_name'], $avatarPath)) {
59
            return false;
60
        }
61
        /* @var  $avt_handler XoopsAvatarHandler */
62
        $avt_handler = xoops_getHandler('avatar');
63
        $avatar = $avt_handler->create();
64
        
65
        $avatar->setVar('avatar_file', 'avatars/' . $avatarName);
66
        $avatar->setVar('avatar_name', $avatarNicename);
67
        $avatar->setVar('avatar_mimetype', $mimeType);
68
        $avatar->setVar('avatar_created', time());
69
        $avatar->setVar('avatar_display', 1);
70
        $avatar->setVar('avatar_weight', 0);
71
        $avatar->setVar('avatar_type', 's');
72
        if (!$avt_handler->insert($avatar)) {
73
            return array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('error' => ...etVar('avatar_name'))); (array<string,string>) is incompatible with the return type of the parent method SystemFineUploadHandler::storeUploadedFile of type array|false.

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
                'error' => sprintf(_FAILSAVEIMG, $avatar->getVar('avatar_name'))
75
            );
76
        }
77
        return array('success'=> true, "uuid" => $uuid);
78
    }
79
}
80