Completed
Pull Request — master (#4)
by mw
01:48
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
		// HHVM ... Fatal error: Call to undefined function
55 2
		if ( !function_exists( 'register_tick_function' ) ) {
56
			return is_array( $args ) ? call_user_func_array( $func, $args ): call_user_func( $func );
57
		}
58
59 2
		declare( ticks=1 );
60
61 2
		self::$memory = memory_get_usage();
62 2
		self::$max    = 0;
63
64 2
		register_tick_function(
65 2
			'call_user_func_array',
66 2
			array( '\Onoi\CallbackContainer\CallFuncMemorySniffer', 'memoryTick' ),
67 2
			array()
68 2
		);
69
70 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...
71 2
		$result = is_array( $args ) ? call_user_func_array( $func, $args ): call_user_func( $func );
72 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...
73
74 2
		unregister_tick_function( 'call_user_func_array' );
75
76 2
		return $result;
77
	}
78
79
	/**
80
	 * @since 2.0
81
	 *
82
	 * @return integer
83
	 */
84 2
	public function getMemoryUsed() {
85 2
		return self::$max;
86
	}
87
88
	/**
89
	 * @since 2.0
90
	 *
91
	 * @return float
92
	 */
93 2
	public function getTimeUsed() {
94 2
		return $this->time;
95
	}
96
97
}
98