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

log_page_class::check_page()   C

Complexity

Conditions 12
Paths 14

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 22
nc 14
nop 0
dl 0
loc 33
rs 5.1612
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace eoxia;
3
4
if ( !defined( 'ABSPATH' ) ) exit;
5
6
if ( ! class_exists( '\eoxia\log_page_class' ) ) {
7
	class log_page_class extends \eoxia\Singleton_Util {
8
		protected function construct() {}
9
10
		public function get_array_size_format() {
11
			return array('oc' => 'Octets', 'ko' => 'Ko', 'mo' => 'Mo', 'go' => 'Go');
12
		}
13
14
		public function open_log( $service_name ) {
15
			$upload_dir = wp_upload_dir();
16
			$dir_file = $upload_dir['basedir'] . '/wpeolog/' . $service_name . '.csv';
17
18
			if ( file_exists( $dir_file ) ) {
19
				$file = file( $dir_file );
20
				// Remove the first case empty
21
				array_shift( $file );
22
				foreach( $file as &$data ) {
23
					$data = explode( log_class::$file_separator, $data );
24
					$data[3] = base64_decode( $data[3] );
25
					$data[4] = stripcslashes( $data[4] );
26
				}
27
28
				return array( 'data' => $file, 'count' => count( $file ) );
29
			}
30
			else {
31
				return array( 'data' => null, 'count' => 0 );
32
			}
33
		}
34
35
		public function check_page() {
36
			if( empty( $_GET['action'] ) || empty( $_GET['type'] ) )
37
				return false;
38
39
			$action = sanitize_text_field( $_GET['action'] );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $action is correct as sanitize_text_field($_GET['action']) (which targets sanitize_text_field()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
40
			$type = sanitize_text_field( !empty( $_GET['type'] ) ?  $_GET['type'] : '' );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $type is correct as sanitize_text_field(!emp...) ? $_GET['type'] : '') (which targets sanitize_text_field()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
41
42
			if ( !isset( $_GET['service_id'] ) ) return false;
43
			else $service_id = (int) $_GET['service_id'];
44
			if ( isset( $_GET['key'] ) && 0 !== (int) $_GET['key'] ) $key = (int) $_GET['key'];
45
46
			if ( 'view' == $action && isset( $service_id ) ) {
47
				$service = log_service_class::g()->get_service_by_id( $service_id );
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class eoxia\Singleton_Util as the method get_service_by_id() does only exist in the following sub-classes of eoxia\Singleton_Util: eoxia\log_service_class. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
48
				if( $service == null )
49
					return false;
50
				$service_name = $service['name'];
51
				if ( !empty( $type ) ) {
52
					$service_name .= '-' . $type;
53
				}
54
55
				// Tous les fichiers
56
				$list_archive_file = log_archive_class::g()->get_archive_file( $service_name );
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class eoxia\Singleton_Util as the method get_archive_file() does only exist in the following sub-classes of eoxia\Singleton_Util: eoxia\log_archive_class. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
57
				if( !empty( $key ) ) {
58
					$service_name .= '_' . $key;
59
				}
60
61
				$count_warning = $this->open_log( $service['name'] . '-warning' );
62
				$count_error = $this->open_log( $service['name'] . '-error' );
63
				$array_data = $this->open_log( $service_name );
64
65
				return array( 'data' => $array_data, 'list_archive_file' => $list_archive_file, 'count_warning' => $count_warning['count'], 'count_error' => $count_error['count'] );
66
			}
67
		}
68
69
	}
70
}
71
72
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
73