Completed
Pull Request — master (#4)
by mw
18:43 queued 17:12
created

CallFuncMemorySniffer::getTimeUsed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Onoi\CallbackContainer;
4
5
use RuntimeException;
6
7
/**
8
 * @see http://stackoverflow.com/questions/19973037/benchmark-memory-usage-in-php
9
 *
10
 * @license GNU GPL v2+
11
 * @since 2.0
12
 */
13
class CallFuncMemorySniffer {
14
15
	/**
16
	 * @var integer
17
	 */
18
	private static $max;
19
20
	/**
21
	 * @var integer
22
	 */
23
	private static $memory;
24
25
	/**
26
	 * @var integer
27
	 */
28
	private $time;
29
30
	/**
31
	 * @since 2.0
32
	 */
33 2
	public static function memoryTick() {
34 2
		self::$memory = memory_get_usage() - self::$memory;
35 2
		self::$max    = self::$memory > self::$max ? self::$memory : self::$max;
36 2
		self::$memory = memory_get_usage();
37 2
	}
38
39
	/**
40
	 * @since 2.0
41
	 *
42
	 * @param Closure|callable $func
43
	 * @param array|null $args
44
	 *
45
	 * @return mixed
46
	 * @throws RuntimeException
47
	 */
48 3
	public function call( $func, $args = null ) {
49
50 3
		if ( !is_callable( $func ) ) {
51 1
			throw new RuntimeException( "Function is not callable" );
52
		}
53
54 2
		declare( ticks=1 );
55
56 2
		self::$memory = memory_get_usage();
57 2
		self::$max    = 0;
58
59 2
		register_tick_function(
60 2
			'call_user_func_array',
61 2
			array( '\Onoi\CallbackContainer\CallFuncMemorySniffer', 'memoryTick' ),
62 2
			array()
63 2
		);
64
65 2
		$this->time = microtime( true );
0 ignored issues
show
Documentation Bug introduced by
The property $time 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...
66 2
		$result = is_array( $args ) ? call_user_func_array( $func, $args ): call_user_func( $func );
67 2
		$this->time = microtime( true ) - $this->time;
0 ignored issues
show
Documentation Bug introduced by
The property $time was declared of type integer, but microtime(true) - $this->time 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...
68
69 2
		unregister_tick_function( 'call_user_func_array' );
70
71 2
		return $result;
72
	}
73
74
	/**
75
	 * @since 2.0
76
	 *
77
	 * @return integer
78
	 */
79 2
	public function getMemoryUsed() {
80 2
		return self::$max;
81
	}
82
83
	/**
84
	 * @since 2.0
85
	 *
86
	 * @return float
87
	 */
88 2
	public function getTimeUsed() {
89 2
		return $this->time;
90
	}
91
92
}
93