GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#936)
by Tom
02:54
created

Backup_Status   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 17
c 4
b 0
f 0
lcom 1
cbo 1
dl 0
loc 107
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A start() 0 4 1
A get_backup_filename() 0 12 3
A is_started() 0 3 1
A finish() 0 6 2
A get_status() 0 16 3
A set_status() 0 11 1
A get_start_time() 0 15 4
A get_status_filepath() 0 3 1
1
<?php
2
3
namespace HM\BackUpWordPress;
4
5
/**
6
 * Manages status and progress of a backup
7
 */
8
class Backup_Status {
9
10
  public function __construct( $id ) {
11
    $this->id = $id;
0 ignored issues
show
Bug introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
12
  }
13
14
  public function start( $backup_filename, $status_message ) {
15
      $this->filename = $backup_filename;
0 ignored issues
show
Bug introduced by
The property filename does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
16
      $this->set_status( $status_message );
17
  }
18
19
  public function get_backup_filename() {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
20
21
      if ( $this->is_started() ) {
22
          $status = json_decode( file_get_contents( $this->get_status_filepath() ) );
23
24
          if ( ! empty( $status->filename ) ) {
25
            $this->filename = $status->filename;
26
          }
27
      }
28
29
      return $this->filename;
30
  }
31
32
  public function is_started() {
33
    return (bool) file_exists( $this->get_status_filepath() );
34
  }
35
36
  public function finish() {
37
    // Delete the backup running file
38
    if ( file_exists( $this->get_status_filepath() ) ) {
39
      unlink( $this->get_status_filepath() );
40
    }
41
  }
42
43
  /**
44
   * Get the status of the running backup.
45
   *
46
   * @return string
47
   */
48
  public function get_status() {
49
50
    if ( ! file_exists( $this->get_status_filepath() ) ) {
51
      return '';
52
    }
53
54
55
    $status = json_decode( file_get_contents( $this->get_status_filepath() ) );
56
57
    if ( ! empty( $status->status ) ) {
58
      return $status->status;
59
    }
60
61
    return '';
62
63
  }
64
65
  /**
66
   * Set the status of the running backup
67
   *
68
   * @param string $message
69
   *
70
   * @return null
71
   */
72
  public function set_status( $message ) {
73
74
    $status = json_encode( (object) array(
75
      'filename' => $this->filename,
76
      'started'  => $this->get_start_time(),
77
      'status'   => $message,
78
    ) );
79
80
    file_put_contents( $this->get_status_filepath(), $status );
81
82
  }
83
84
  /**
85
   * Get the time that the current running backup was started
86
   *
87
   * @return int $timestamp
88
   */
89
  public function get_start_time() {
90
91
    if ( ! file_exists( $this->get_status_filepath() ) ) {
92
      return 0;
93
    }
94
95
    $status = json_decode( file_get_contents( $this->get_status_filepath() ) );
96
97
    if ( ! empty( $status->started ) && (int) (string) $status->started === $status->started ) {
98
      return $status->started;
99
    }
100
101
    return time();
102
103
  }
104
105
  /**
106
    * Get the path to the backup running file that stores the running backup status
107
    *
108
    * @return string
109
    */
110
    public function get_status_filepath() {
111
    	return Path::get_path() . '/.backup-' . $this->id . '-running';
112
    }
113
114
}
115