Completed
Push — master ( eef885...465d0c )
by
unknown
30:08 queued 12:49
created

File_Util   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A construct() 0 1 1
B move_file_and_attach() 0 30 4
1
<?php
2
/**
3
 * Gestion des fichiers
4
 *
5
 * @package Evarisk\Plugin
6
 */
7
8
namespace eoxia;
9
10
if ( ! defined( 'ABSPATH' ) ) {
11
	exit;
12
}
13
14
if ( ! class_exists( '\eoxia\File_Util' ) ) {
15
	/**
16
	 * Gestion des fichiers
17
	 *
18
	 * @author Jimmy Latour <[email protected]>
19
	 * @version 1.1.0.0
20
	 */
21
	class File_Util extends \eoxia\Singleton_Util {
22
		/**
23
		 * Le constructeur obligatoirement pour utiliser la classe \eoxia\Singleton_Util
24
		 *
25
		 * @return void nothing
26
		 */
27
		protected function construct() {}
28
29
		/**
30
		 * Upload le fichier $file et créer les méta données de ce fichier.
31
		 *
32
		 * @param  mixed $file        Les données du fichier.
33
		 * @param  int   $element_id  L'ID de l'élément pour l'attachement du fichier.
34
		 * @return int            		L'id de l'attachement
35
		 */
36
		public static function move_file_and_attach( $file, $element_id ) {
37
			if ( ! is_string( $file ) || ! is_int( $element_id ) || ! is_file( $file ) ) {
38
				return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by eoxia\File_Util::move_file_and_attach of type integer.

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...
39
			}
40
41
			$wp_upload_dir = wp_upload_dir();
42
43
			// Transfère le thumbnail.
44
			$upload_result = wp_upload_bits( basename( $file ), null, file_get_contents( $file ) );
45
46
			$filetype = wp_check_filetype( basename( $upload_result['file'] ), null );
47
			/**	Set the default values for the current attachement	*/
48
			$attachment_default_args = array(
49
					'guid'           => $wp_upload_dir['url'] . '/' . basename( $upload_result['file'] ),
50
					'post_mime_type' => $filetype['type'],
51
					'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $upload_result['file'] ) ),
52
					'post_content'   => '',
53
					'post_status'    => 'inherit',
54
			);
55
56
			/**	Save new picture into database	*/
57
			$attach_id = wp_insert_attachment( $attachment_default_args, $upload_result['file'], $element_id );
58
59
			/**	Create the different size for the given picture and get metadatas for this picture	*/
60
			$attach_data = wp_generate_attachment_metadata( $attach_id, $upload_result['file'] );
61
			/**	Finaly save pictures metadata	*/
62
			wp_update_attachment_metadata( $attach_id, $attach_data );
63
64
			return $attach_id;
65
		}
66
	}
67
} // End if().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
68