Skip to content

Commit 8261094

Browse files
committed
psalm fixes, missing tests
1 parent 3874503 commit 8261094

23 files changed

+280
-169
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33

44
[![Coverage Status](https://coveralls.io/repos/emgiezet/errbitPHP/badge.png)](https://coveralls.io/r/emgiezet/errbitPHP)
5-
[![Build Status](https://travis-ci.org/emgiezet/errbitPHP.png?branch=master)](https://travis-ci.org/emgiezet/errbitPHP)
65
[![Dependency Status](https://www.versioneye.com/user/projects/5249e725632bac0a4900b2bf/badge.png)](https://www.versioneye.com/user/projects/5249e725632bac0a4900b2bf)
76
[![Latest Stable Version](https://poser.pugx.org/emgiezet/errbit-php/v/stable.png)](https://packagist.org/packages/emgiezet/errbit-php)
87
[![SymfonyInsight](https://insight.symfony.com/projects/a0c405fb-8ee9-40e9-acf1-eee084fc35a6/mini.svg)](https://insight.symfony.com/projects/a0c405fb-8ee9-40e9-acf1-eee084fc35a6)

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"require": {
3131
"php": "^8.0||^8.1",
3232
"guzzlehttp/guzzle": "^7.5.0",
33-
"ext-simplexml": "*"
33+
"ext-simplexml": "*",
34+
"ext-dom": "*"
3435
},
3536
"require-dev": {
3637
"rector/rector": "^0.15.10",

src/Errbit/Errbit.php

+6-9
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33
namespace Errbit;
44

55
use Errbit\Errors\ErrorInterface;
6-
use Errbit\Errors\Warning;
76
use Errbit\Exception\ConfigurationException;
87
use Errbit\Exception\Exception;
98

10-
use Errbit\Errors\Notice;
11-
use Errbit\Errors\Error;
12-
use Errbit\Errors\Fatal;
139
use Errbit\Handlers\ErrorHandlers;
10+
use Errbit\Writer\SocketWriter;
1411
use Errbit\Writer\WriterInterface;
1512

1613
/**
@@ -31,15 +28,15 @@ class Errbit
3128
* @var WriterInterface
3229
*/
3330
protected WriterInterface $writer;
34-
31+
3532
/**
3633
* Get a singleton instance of the client.
3734
*
3835
* This is the intended way to access the Errbit client.
3936
*
40-
* @return Errbit a singleton
37+
* @return \Errbit\Errbit|null a singleton
4138
*/
42-
public static function instance()
39+
public static function instance(): ?Errbit
4340
{
4441
if (!isset(self::$instance)) {
4542
self::$instance = new self();
@@ -226,7 +223,7 @@ private function checkConfig(): void
226223
}
227224

228225
if (!isset($this->config['secure'])) {
229-
$this->config['secure'] = ($this->config['port'] == 443);
226+
$this->config['secure'] = ($this->config['port'] === 443);
230227
}
231228

232229
if (empty($this->config['hostname'])) {
@@ -262,7 +259,7 @@ private function checkConfig(): void
262259
}
263260

264261
if (!isset($this->config['default_writer'])) {
265-
$this->config['default_writer'] = \Errbit\Writer\SocketWriter::class;
262+
$this->config['default_writer'] = SocketWriter::class;
266263
}
267264

268265
if (!isset($this->config['agent'])) {

src/Errbit/Exception/Notice.php

+32-33
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ public function __construct(
4545
/**
4646
* Building request url
4747
*
48-
* @return string url
49-
*
48+
* @return string|null url
5049
*/
5150
private function buildRequestUrl(): ?string
5251
{
@@ -72,11 +71,13 @@ private function guessProtocol(): string
7271
{
7372
if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
7473
return $_SERVER['HTTP_X_FORWARDED_PROTO'];
75-
} elseif (!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443) {
74+
}
75+
76+
if (!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] === 443) {
7677
return 'https';
77-
} else {
78-
return 'http';
7978
}
79+
80+
return 'http';
8081
}
8182

8283
/**
@@ -88,11 +89,13 @@ private function guessHost(): string
8889
{
8990
if (!empty($_SERVER['HTTP_HOST'])) {
9091
return $_SERVER['HTTP_HOST'];
91-
} elseif (!empty($_SERVER['SERVER_NAME'])) {
92+
}
93+
94+
if (!empty($_SERVER['SERVER_NAME'])) {
9295
return $_SERVER['SERVER_NAME'];
93-
} else {
94-
return '127.0.0.1';
9596
}
97+
98+
return '127.0.0.1';
9699
}
97100

98101
/**
@@ -180,13 +183,12 @@ public function asXml(): string
180183
$exception = $this->exception;
181184
$options = $this->options;
182185
$builder = new XmlBuilder();
183-
$self = $this;
184-
186+
185187
return $builder->tag(
186188
'notice',
187189
'',
188190
['version' => Errbit::API_VERSION],
189-
function (XmlBuilder $notice) use ($exception, $options, $self) {
191+
function (XmlBuilder $notice) use ($exception, $options) {
190192
$notice->tag('api-key', $options['api_key']);
191193
$notice->tag(
192194
'notifier',
@@ -203,7 +205,8 @@ function (XmlBuilder $notifier) {
203205
'error',
204206
'',
205207
[],
206-
function (XmlBuilder $error) use ($exception, $self) {
208+
function (XmlBuilder $error) use ($exception) {
209+
$self = $this;
207210
$class = Notice::className($exception);
208211
$error->tag('class', $self->filterTrace($class));
209212
$error->tag(
@@ -373,7 +376,7 @@ function (XmlBuilder $env) use ($options) {
373376
}
374377

375378
/**
376-
* Get a human readable class name for the Exception.
379+
* Get a human-readable class name for the Exception.
377380
*
378381
* Native PHP errors are named accordingly.
379382
*
@@ -384,18 +387,14 @@ function (XmlBuilder $env) use ($options) {
384387
public static function className(object $exception): string
385388
{
386389
$shortClassname = self::parseClassname($exception::class);
387-
switch ($shortClassname['classname']) {
388-
case 'Notice':
389-
return 'Notice';
390-
case 'Warning':
391-
return 'Warning';
392-
case 'Error':
393-
return 'Error';
394-
case 'Fatal':
395-
return 'Fatal Error';
396-
default:
397-
return $shortClassname['classname'];
398-
}
390+
391+
return match ($shortClassname['classname']) {
392+
'Notice' => 'Notice',
393+
'Warning' => 'Warning',
394+
'Error' => 'Error',
395+
'Fatal' => 'Fatal Error',
396+
default => $shortClassname['classname'],
397+
};
399398
}
400399

401400
/**
@@ -454,16 +453,16 @@ public static function formatMethod(array $frame): string
454453
$frame['type'],
455454
$frame['function']
456455
);
457-
} else {
458-
return sprintf(
459-
'%s()',
460-
!empty($frame['function']) ? $frame['function'] : '<unknown>'
461-
);
462456
}
457+
458+
return sprintf(
459+
'%s()',
460+
!empty($frame['function']) ? $frame['function'] : '<unknown>'
461+
);
463462
}
464463

465464
/**
466-
* Recursively build an list of the all the vars in the given array.
465+
* Recursively build a list of the all the vars in the given array.
467466
*
468467
* @param \Errbit\Utils\XmlBuilder $builder the builder instance to set the
469468
* data into
@@ -485,7 +484,7 @@ public static function xmlVarsFor(XmlBuilder $builder, array $array): void
485484
}
486485

487486
if (is_array($value)) {
488-
if (null === $hash || !in_array($hash, self::$hashArray)) {
487+
if (null === $hash || !in_array($hash, self::$hashArray, true)) {
489488
self::$hashArray[] = $hash;
490489
$builder->tag(
491490
'var',
@@ -505,7 +504,7 @@ function ($var) use ($value) {
505504
}
506505

507506
} else {
508-
$builder->tag('var', $value, ['key' => $key]);
507+
$builder->tag('var', (string) $value, ['key' => $key]);
509508
}
510509
}
511510
}

src/Errbit/Handlers/ErrorHandlers.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
use Errbit\Errbit;
77
use Errbit\Errors\Fatal;
8-
use Errbit\Exception\Exception;
98
use Errbit\Utils\Converter;
9+
use Exception;
1010

1111
/**
1212
*
@@ -64,7 +64,7 @@ public function onError(int $code, string $message, string $file, int $line): vo
6464
*
6565
* @throws \Exception
6666
*/
67-
public function onException(\Exception $exception): void
67+
public function onException(Exception $exception): void
6868
{
6969
$error = $this->converter->convert($exception->getCode(), $exception->getMessage(), $exception->getFile(),
7070
$exception->getLine(), debug_backtrace());

src/Errbit/Utils/XmlBuilder.php

+10-11
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@
2828
class XmlBuilder
2929
{
3030
private \SimpleXMLElement $_xml;
31+
3132
/**
3233
* Instantiate a new XmlBuilder.
3334
*
34-
* @param SimpleXMLElement $xml the parent node (only used internally)
35+
* @param \SimpleXMLElement|null $xml the parent node (only used internally)
3536
*/
3637
public function __construct(?\SimpleXMLElement $xml = null)
3738
{
@@ -51,22 +52,20 @@ public function __construct(?\SimpleXMLElement $xml = null)
5152
/**
5253
* Insert a tag into the XML.
5354
*
54-
* @param string $name the name of the tag, required.
55-
* @param string $value the text value of the element, optional
56-
* @param array $attributes an array of attributes for the tag, optional
57-
* @param Callable $callback a callback to receive an XmlBuilder for the new tag, optional
55+
* @param string $name the name of the tag, required.
56+
* @param string $value the text value of the element, optional
57+
* @param array $attributes an array of attributes for the tag, optional
58+
* @param Callable|null $callback a callback to receive an XmlBuilder for the new tag, optional
5859
*
5960
* @return XmlBuilder a builder for the inserted tag
6061
*/
61-
public function tag($name, $value = '', $attributes = [], $callback = null, bool $getLastChild = false)
62+
public function tag(string $name, string $value = '', array $attributes = [], callable $callback = null, bool $getLastChild = false): XmlBuilder
6263
{
6364

6465
$idx = is_countable($this->_xml->$name) ? count($this->_xml->$name) : 0;
6566

6667
if (is_object($value)) {
6768
$value = "[" . $value::class . "]";
68-
} else {
69-
$value = (string) $value;
7069
}
7170

7271
$this->_xml->{$name}[$idx] = $value;
@@ -96,7 +95,7 @@ public function tag($name, $value = '', $attributes = [], $callback = null, bool
9695
*
9796
* @return static the current builder
9897
*/
99-
public function attribute($name, $value): static
98+
public function attribute(string $name, string $value): static
10099
{
101100
$this->_xml[$name] = $value;
102101

@@ -116,11 +115,11 @@ public function asXml(): string
116115
/**
117116
* Util to converts special chars to be valid with xml
118117
*
119-
* @param string $string xml string to converte the special chars
118+
* @param string $string xml string to convert the special chars
120119
*
121120
* @return string escaped string
122121
*/
123-
public static function utf8ForXML($string)
122+
public static function utf8ForXML(string $string): string
124123
{
125124
return preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $string);
126125
}

src/Errbit/Writer/AbstractWriter.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ abstract class AbstractWriter
1111
* Hoptoad Notifier Route
1212
*/
1313
public const NOTICES_PATH = '/notifier_api/v2/notices/';
14+
1415
/**
16+
* @param array $config
17+
*
1518
* @return string
1619
*/
1720
protected function buildConnectionScheme(array $config): string
1821
{
19-
$proto = "";
2022
if ($config['async']) {
2123
$proto = "udp";
2224
} elseif ($config['secure']) {

src/Errbit/Writer/GuzzleWriter.php

+6-9
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22
declare(strict_types=1);
33
namespace Errbit\Writer;
44

5-
use Errbit\Errors\Error;
65
use Errbit\Errors\ErrorInterface;
7-
use Errbit\Errors\Fatal;
8-
use Errbit\Errors\Notice;
9-
use Errbit\Errors\Warning;
10-
use Exception;
116
use GuzzleHttp\ClientInterface;
127
use GuzzleHttp\Promise\PromiseInterface;
138
use Psr\Http\Message\ResponseInterface;
@@ -38,11 +33,13 @@ public function __construct(private ClientInterface $client)
3833
}
3934

4035
/**
41-
* @param \Exception $exception
36+
* @param \Errbit\Errors\ErrorInterface $exception
37+
* @param array $config
4238
*
39+
* @return \Psr\Http\Message\ResponseInterface|\GuzzleHttp\Promise\PromiseInterface|null
4340
* @throws \GuzzleHttp\Exception\GuzzleException
4441
*/
45-
public function write(ErrorInterface $exception, array $config): ResponseInterface|PromiseInterface
42+
public function write(ErrorInterface $exception, array $config): null|ResponseInterface|PromiseInterface
4643
{
4744
if($config['async']) {
4845
return $this->asyncWrite($exception, $config);
@@ -76,7 +73,8 @@ public function synchronousWrite(ErrorInterface $exception, array $config): Res
7673
public function asyncWrite(ErrorInterface $exception, array $config): PromiseInterface
7774
{
7875
$uri = $this->buildConnectionScheme($config);
79-
$promise = $this->client->postAsync(
76+
77+
return $this->client->postAsync(
8078
$uri.self::NOTICES_PATH,
8179
[
8280
'body' =>$this->buildNoticeFor($exception, $config),
@@ -87,6 +85,5 @@ public function asyncWrite(ErrorInterface $exception, array $config): PromiseInt
8785
]
8886
]
8987
);
90-
return $promise;
9188
}
9289
}

0 commit comments

Comments
 (0)