Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PHPStan at level 6 #637

Merged
merged 29 commits into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
3e5039b
Add phpstan
xPaw Jan 31, 2022
f36398a
Fix up types where possible
xPaw Jan 31, 2022
7c612eb
Add PHPStan config file
imjoehaines Jan 31, 2022
f07bf15
Ignore unsafe 'new static' calls for now
imjoehaines Dec 21, 2021
f6866e4
Fix Report::setSessionData return type
imjoehaines Jan 31, 2022
412ab8c
Run PHPStan on CI
imjoehaines Dec 21, 2021
2750ab2
Ignore errors from old Guzzle versions for now
imjoehaines Dec 21, 2021
876c4ac
Explicitly return null from non-void methods
imjoehaines Jan 31, 2022
9b8b56f
Correct the type of 'numLines'
imjoehaines Jan 31, 2022
fcbaca5
Correct parameter name
imjoehaines Jan 31, 2022
80fc98a
Fix use of @var instead of @return
imjoehaines Jan 31, 2022
b2dbecb
Bump PHPStan to level 2
imjoehaines Jan 31, 2022
daeda01
Correct return types
imjoehaines Jan 31, 2022
58f944b
Explicitly return null
imjoehaines Jan 31, 2022
4ccb5a9
Bump PHPStan to level 3
imjoehaines Jan 31, 2022
e29b688
Ignore errors from PHP 5 compatibility code
imjoehaines Jan 31, 2022
e13c8ca
Ignore an error that isn't fixable
imjoehaines Jan 31, 2022
3c42e65
Fix types claiming to be non-nullable
imjoehaines Jan 31, 2022
d1d1f17
Bump PHPStan to level 4
imjoehaines Jan 31, 2022
82b076f
Fix passing an int to ini_set
imjoehaines Dec 21, 2021
a4cbb88
Fix type of 'topFile' in Stacktrace::fromBacktrace
imjoehaines Jan 31, 2022
14ac5a7
Fix passing null in preg_match
xPaw Nov 18, 2021
0795909
Bump PHPStan to level 5
imjoehaines Jan 31, 2022
7e30518
Add key & value types for the breadcrumb Recorder
imjoehaines Jan 4, 2022
914cb46
Add missing return types
imjoehaines Jan 31, 2022
7413ecd
Add missing property types
imjoehaines Jan 31, 2022
6a348f1
Add missing parameter type
imjoehaines Jan 31, 2022
8ed6c61
Bump PHPStan to level 6
imjoehaines Jan 4, 2022
153fb09
Add changelog entry
imjoehaines Jan 31, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .github/workflows/test-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,42 @@ jobs:
run: composer update --prefer-dist --no-progress --no-suggest --no-interaction ${{ matrix.composer-flags }}

- run: composer run-script test

phpstan:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
# TODO: we likely have to care about Guzzle's version too - ideally we'd
# check our use of Guzzle is correct on each version
php-version: ['7.1', '8.1']
phpstan-version: ['~1.4']

steps:
- uses: actions/checkout@v2

- name: install PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
coverage: none
extensions: intl, mbstring
# by default setup-php uses a production php.ini so force development values
ini-values: >-
zend.exception_ignore_args=Off,
zend.exception_string_param_max_len=15,
error_reporting=-1,
display_errors=On,
display_startup_errors=On,
zend.assertions=1

# - name: require guzzle
# run: composer require "guzzlehttp/guzzle:${{ matrix.guzzle-version }}" --no-update ${{ matrix.composer-flags }}

- name: require phpstan
run: composer require "phpstan/phpstan:${{ matrix.phpstan-version }}" --no-update ${{ matrix.composer-flags }}

- name: install dependencies
run: composer update --prefer-dist --no-progress --no-suggest --no-interaction ${{ matrix.composer-flags }}

- run: ./vendor/bin/phpstan
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
Changelog
=========

## TBD

### Fixes

* A number of errors in docblocks have been corrected
[xPaw](https://github.com/xPaw)
[#633](https://github.com/bugsnag/bugsnag-php/pull/633)
[#637](https://github.com/bugsnag/bugsnag-php/pull/637)

## 3.26.1 (2021-09-09)

### Fixes
Expand Down
14 changes: 14 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
parameters:
level: 6

treatPhpDocTypesAsCertain: false
checkMissingIterableValueType: false # TODO: remove this!

paths:
- src/

ignoreErrors:
-
message: "#^Property Bugsnag\\\\Handler\\:\\:\\$reservedMemory is never read, only written\\.$#"
count: 1
path: src/Handler.php
5 changes: 4 additions & 1 deletion src/Breadcrumbs/Recorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use Countable;
use Iterator;

/**
* @implements Iterator<int, Breadcrumb>
*/
class Recorder implements Countable, Iterator
{
/**
Expand Down Expand Up @@ -138,7 +141,7 @@ public function rewind()
/**
* Is the current key position set?
*
* @return int
* @return bool
*/
#[\ReturnTypeWillChange]
public function valid()
Expand Down
15 changes: 9 additions & 6 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public static function make(
$config = new Configuration($apiKey ?: $env->get('BUGSNAG_API_KEY'));
$guzzle = static::makeGuzzle($notifyEndpoint ?: $env->get('BUGSNAG_ENDPOINT'));

// @phpstan-ignore-next-line
$client = new static($config, null, $guzzle);

if ($defaults) {
Expand Down Expand Up @@ -470,7 +471,7 @@ public function getSessionTracker()
/**
* Get the Bugsnag API Key.
*
* @var string
* @return string
*/
public function getApiKey()
{
Expand Down Expand Up @@ -550,7 +551,7 @@ public function setFilters(array $filters)
*
* @deprecated Use redactedKeys instead
*
* @var string[]
* @return string[]
*/
public function getFilters()
{
Expand Down Expand Up @@ -586,7 +587,7 @@ public function setProjectRootRegex($projectRootRegex)
*
* @param string $file
*
* @return string
* @return bool
*/
public function isInProject($file)
{
Expand Down Expand Up @@ -674,7 +675,7 @@ public function setNotifier(array $notifier)
/**
* Get the notifier to report as to Bugsnag.
*
* @var string[]
* @return string[]
*/
public function getNotifier()
{
Expand Down Expand Up @@ -948,6 +949,8 @@ public function getSessionClient()
* This is an amount of bytes or 'null' to disable increasing the limit.
*
* @param int|null $value
*
* @return Configuration
*/
public function setMemoryLimitIncrease($value)
{
Expand Down Expand Up @@ -985,7 +988,7 @@ public function setDiscardClasses(array $discardClasses)
*
* This can contain both fully qualified class names and regular expressions.
*
* @var array
* @return array
*/
public function getDiscardClasses()
{
Expand All @@ -1009,7 +1012,7 @@ public function setRedactedKeys(array $redactedKeys)
/**
* Get the array of metadata keys that should be redacted.
*
* @var string[]
* @return string[]
*/
public function getRedactedKeys()
{
Expand Down
20 changes: 11 additions & 9 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class Configuration
/**
* A client to use to send sessions.
*
* @var \GuzzleHttp\ClientInterface
* @var \GuzzleHttp\ClientInterface|null
*
* @deprecated This will be removed in the next major version.
*/
Expand Down Expand Up @@ -204,7 +204,7 @@ public function __construct($apiKey)
/**
* Get the Bugsnag API Key.
*
* @var string
* @return string
*/
public function getApiKey()
{
Expand Down Expand Up @@ -288,7 +288,7 @@ public function setFilters(array $filters)
*
* @deprecated Use redactedKeys instead
*
* @var string[]
* @return string[]
*/
public function getFilters()
{
Expand Down Expand Up @@ -317,7 +317,7 @@ public function setProjectRoot($projectRoot)
*/
public function setProjectRootRegex($projectRootRegex)
{
if ($projectRootRegex && @preg_match($projectRootRegex, null) === false) {
if ($projectRootRegex && @preg_match($projectRootRegex, '') === false) {
throw new InvalidArgumentException('Invalid project root regex: '.$projectRootRegex);
}

Expand All @@ -330,7 +330,7 @@ public function setProjectRootRegex($projectRootRegex)
*
* @param string $file
*
* @return string
* @return bool
*/
public function isInProject($file)
{
Expand Down Expand Up @@ -359,7 +359,7 @@ public function setStripPath($stripPath)
*/
public function setStripPathRegex($stripPathRegex)
{
if ($stripPathRegex && @preg_match($stripPathRegex, null) === false) {
if ($stripPathRegex && @preg_match($stripPathRegex, '') === false) {
throw new InvalidArgumentException('Invalid strip path regex: '.$stripPathRegex);
}

Expand Down Expand Up @@ -423,7 +423,7 @@ public function setNotifier(array $notifier)
/**
* Get the notifier to report as to Bugsnag.
*
* @var string[]
* @return string[]
*/
public function getNotifier()
{
Expand Down Expand Up @@ -804,6 +804,8 @@ public function getSessionClient()
* This is an amount of bytes or 'null' to disable increasing the limit.
*
* @param int|null $value
*
* @return $this
*/
public function setMemoryLimitIncrease($value)
{
Expand Down Expand Up @@ -843,7 +845,7 @@ public function setDiscardClasses(array $discardClasses)
*
* This can contain both fully qualified class names and regular expressions.
*
* @var array
* @return array
*/
public function getDiscardClasses()
{
Expand All @@ -867,7 +869,7 @@ public function setRedactedKeys(array $redactedKeys)
/**
* Get the array of metadata keys that should be redacted.
*
* @var string[]
* @return string[]
*/
public function getRedactedKeys()
{
Expand Down
7 changes: 6 additions & 1 deletion src/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public static function register($client = null)
$client = Client::make($client);
}

// @phpstan-ignore-next-line
$handler = new static($client);
$handler->registerBugsnagHandlers(true);

Expand Down Expand Up @@ -196,6 +197,9 @@ public function exceptionHandler($throwable)

return;
} catch (Throwable $exceptionFromPreviousHandler) {
// TODO: if we drop support for PHP 5, we can remove this catch, which
// fixes the PHPStan issue here
// @phpstan-ignore-next-line
} catch (Exception $exceptionFromPreviousHandler) {
}

Expand Down Expand Up @@ -307,8 +311,9 @@ public function shutdownHandler()
&& preg_match($this->oomRegex, $lastError['message'], $matches) === 1
) {
$currentMemoryLimit = (int) $matches[1];
$newMemoryLimit = $currentMemoryLimit + $this->client->getMemoryLimitIncrease();

ini_set('memory_limit', $currentMemoryLimit + $this->client->getMemoryLimitIncrease());
ini_set('memory_limit', (string) $newMemoryLimit);
}

// Check if a fatal error caused this shutdown
Expand Down
6 changes: 4 additions & 2 deletions src/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,16 @@ protected function getHeaders($version = self::NOTIFY_PAYLOAD_VERSION)
/**
* Send a POST request to Bugsnag.
*
* @param string $uri the uri to hit
* @param array $data the request options
* @param string $uri the uri to hit
* @param array $options the request options
*
* @return void
*/
protected function post($uri, array $options = [])
{
if (GuzzleCompat::isUsingGuzzle5()) {
// TODO: validate this by running PHPStan with Guzzle 5
// @phpstan-ignore-next-line
$this->guzzle->post($uri, $options);
} else {
$this->guzzle->request('POST', $uri, $options);
Expand Down
3 changes: 2 additions & 1 deletion src/Internal/GuzzleCompat.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ public static function getBaseUriOptionName()
*/
public static function getBaseUri(GuzzleHttp\ClientInterface $guzzle)
{
// TODO: validate this by running PHPStan with Guzzle 5
return self::isUsingGuzzle5()
? $guzzle->getBaseUrl()
? $guzzle->getBaseUrl() // @phpstan-ignore-line
: $guzzle->getConfig(self::getBaseUriOptionName());
}

Expand Down
16 changes: 12 additions & 4 deletions src/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class Report
/**
* Attached session from SessionTracking.
*
* @var array
* @var array|null
*/
protected $session;

Expand All @@ -143,6 +143,7 @@ class Report
*/
public static function fromPHPError(Configuration $config, $code, $message, $file, $line, $fatal = false)
{
// @phpstan-ignore-next-line
$report = new static($config);

$report->setPHPError($code, $message, $file, $line, $fatal)
Expand All @@ -162,6 +163,7 @@ public static function fromPHPError(Configuration $config, $code, $message, $fil
*/
public static function fromPHPThrowable(Configuration $config, $throwable)
{
// @phpstan-ignore-next-line
$report = new static($config);

$report->setPHPThrowable($throwable)
Expand All @@ -182,6 +184,7 @@ public static function fromPHPThrowable(Configuration $config, $throwable)
*/
public static function fromNamedError(Configuration $config, $name, $message = null)
{
// @phpstan-ignore-next-line
$report = new static($config);

$report->setName($name)
Expand Down Expand Up @@ -229,6 +232,9 @@ public function getOriginalError()
*/
public function setPHPThrowable($throwable)
{
// TODO: if we drop support for PHP 5, we can remove this check for
// 'Exception', which fixes the PHPStan issue here
// @phpstan-ignore-next-line
if (!$throwable instanceof Throwable && !$throwable instanceof Exception) {
throw new InvalidArgumentException('The throwable must implement Throwable or extend Exception.');
}
Expand Down Expand Up @@ -331,6 +337,8 @@ public function setSeverityReason(array $severityReason)
/**
* Sets the unhandled flag.
*
* @param bool $unhandled
*
* @return $this
*/
public function setUnhandled($unhandled)
Expand Down Expand Up @@ -637,7 +645,7 @@ public function getSummary()
/**
* Sets the session data.
*
* @return $this
* @return void
*/
public function setSessionData(array $session)
{
Expand Down Expand Up @@ -746,12 +754,12 @@ protected function exceptionObject()
* @param mixed $obj the data to cleanup
* @param bool $isMetaData if it is meta data
*
* @return array|null
* @return mixed
*/
protected function cleanupObj($obj, $isMetaData)
{
if (is_null($obj)) {
return;
return null;
}

if (is_array($obj)) {
Expand Down
Loading