Passed
Push — master ( 8b0d37...3bc1e0 )
by Christoph
02:00 queued 11s
created

RecursionAwareReporter::guard()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright 2020 Christoph Wurst <[email protected]>
7
 *
8
 * @author 2020 Christoph Wurst <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 */
25
26
namespace OCA\Sentry\Reporter;
27
28
use OCP\Support\CrashReport\ICollectBreadcrumbs;
29
use OCP\Support\CrashReport\IMessageReporter;
30
use OCP\Support\CrashReport\IReporter;
31
32
/**
33
 * Decorator that detects and stops recursive calls to reporter methods
34
 */
35
class RecursionAwareReporter implements IMessageReporter, ICollectBreadcrumbs {
36
37
	/** @var IReporter */
38
	private $reporter;
39
40
	private $reporting = false;
41
42
	public function __construct(IReporter $reporter) {
43
		$this->reporter = $reporter;
44
	}
45
46
	private function guard(callable $run): void {
47
		try {
48
			if ($this->reporting) {
49
				// Break the recursion
50
				return;
51
			}
52
			$this->reporting = true;
53
			$run();
54
		} finally {
55
			$this->reporting = false;
56
		}
57
	}
58
59
	public function collect(string $message, string $category, array $context = []): void {
60
		if ($this->reporter instanceof ICollectBreadcrumbs) {
61
			$this->guard(function() use ($context, $category, $message) {
62
				$this->reporter->collect($message, $category, $context);
0 ignored issues
show
Bug introduced by
The method collect() does not exist on OCP\Support\CrashReport\IReporter. It seems like you code against a sub-type of said class. However, the method does not exist in OCP\Support\CrashReport\IMessageReporter. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
				$this->reporter->/** @scrutinizer ignore-call */ 
63
                     collect($message, $category, $context);
Loading history...
63
			});
64
		}
65
	}
66
67
	public function reportMessage(string $message, array $context = []): void {
68
		if ($this->reporter instanceof IMessageReporter) {
69
			$this->guard(function() use ($context, $message) {
70
				$this->reporter->reportMessage($message, $context);
0 ignored issues
show
Bug introduced by
The method reportMessage() does not exist on OCP\Support\CrashReport\IReporter. Did you maybe mean report()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
				$this->reporter->/** @scrutinizer ignore-call */ 
71
                     reportMessage($message, $context);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
			});
72
		}
73
	}
74
75
	public function report($exception, array $context = []): void {
76
		$this->guard(function() use ($context, $exception) {
77
			$this->reporter->report($exception, $context);
78
		});
79
	}
80
81
}
82