Completed
Push — kraftbj-patch-2 ( 82c983...ae9d16 )
by
unknown
513:58 queued 503:20
created

Jetpack_Sitemap_Logger::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * A message logger for the Jetpack Sitemap module.
4
 *
5
 * @package Jetpack
6
 * @since 4.7.0
7
 */
8
9
/**
10
 * Handles logging errors and debug messages for sitemap generator.
11
 *
12
 * A Jetpack_Sitemap_Logger object keeps track of its birth time as well
13
 * as a "unique" ID string. Calling the report() method writes a message
14
 * to the PHP error log as well as the ID string for easier grepping.
15
 *
16
 * @since 4.7.0
17
 */
18
class Jetpack_Sitemap_Logger {
19
	/**
20
	 * A unique-ish string for each logger, enabling us to grep
21
	 * for the messages written by an individual generation phase.
22
	 *
23
	 * @access private
24
	 * @since 4.7.0
25
	 * @var string $key The key string.
26
	 */
27
	private $key;
28
29
	/**
30
	 * The birth time of this object in microseconds.
31
	 *
32
	 * @access private
33
	 * @since 4.7.0
34
	 * @var int $starttime The birth time.
35
	 */
36
	private $starttime;
37
38
	/**
39
	 * Initializes a new logger object.
40
	 *
41
	 * @access public
42
	 * @since 4.7.0
43
	 *
44
	 * @param string $message An optional message string to be written to the debug log on initialization.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $message not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
45
	 */
46
	public function __construct( $message = null ) {
47
		$this->key = wp_generate_password( 5, false );
48
		$this->starttime = microtime( true );
0 ignored issues
show
Documentation Bug introduced by
The property $starttime was declared of type integer, but microtime(true) is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
49
		if ( ! is_null( $message ) ) {
50
			$this->report( $message );
51
		}
52
		return;
53
	}
54
55
	/**
56
	 * Writes a string to the debug log, including the logger's ID string.
57
	 *
58
	 * @access public
59
	 * @since 4.7.0
60
	 *
61
	 * @param string $message The string to be written to the log.
62
	 */
63
	public function report( $message ) {
64
		if ( defined( 'WP_DEBUG' ) && ( true === WP_DEBUG ) ) {
65
			error_log( 'jp-sitemap-' . $this->key . ': ' . $message );
66
		}
67
		return;
68
	}
69
70
	/**
71
	 * Writes the elapsed lifetime of the logger to the debug log, with an optional message.
72
	 *
73
	 * @access public
74
	 * @since 4.7.0
75
	 *
76
	 * @param string $message The optional message string. Default is the empty string.
77
	 */
78
	public function time( $message = '' ) {
79
		$time = round( microtime( true ) - $this->starttime, 3 );
80
		$this->report( $message . ' ' . $time . ' seconds elapsed.' );
81
		return;
82
	}
83
}
84