Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
69 changes: 69 additions & 0 deletions Tests/ImplicitlyNullableParametersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/**
* @copyright Copyright (C) 2005 - 2026 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

namespace Joomla\Github\Tests;

use PHPUnit\Framework\TestCase;

/**
* Checks that no parameter is implicitly nullable, which PHP 8.4 deprecates.
*/
class ImplicitlyNullableParametersTest extends TestCase
{
public function testNoImplicitlyNullableParameters()
{
$found = [];
$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(\dirname(__DIR__) . '/src'));

foreach ($files as $file) {
if ($file->getExtension() !== 'php') {
continue;
}

foreach ((new \ReflectionClass($this->getClassName($file->getPathname())))->getMethods() as $method) {
if ($method->getFileName() !== $file->getRealPath()) {
continue;
}

foreach ($method->getParameters() as $parameter) {
$type = $parameter->getType();

if (
$type instanceof \ReflectionNamedType
&& $type->getName() !== 'mixed'
&& $parameter->isDefaultValueAvailable()
&& $parameter->getDefaultValue() === null
&& !$this->isDeclaredNullable($method, $parameter)
) {
$found[] = $method->class . '::' . $method->getName() . '() $' . $parameter->getName();
}
}
}
}

$this->assertSame([], $found, 'Implicitly nullable parameters (use ?Type)');
}

private function getClassName(string $path): string
{
$relative = substr($path, \strlen(\dirname(__DIR__) . '/src/'), -4);

return 'Joomla\\Github\\' . str_replace(['/', '\\'], '\\', $relative);
}

/**
* The type of a parameter declared as `?Type $x = null` and as `Type $x = null` is the same in reflection, so look
* at the declaration in the source.
*/
private function isDeclaredNullable(\ReflectionMethod $method, \ReflectionParameter $parameter): bool
{
$lines = \array_slice(file($method->getFileName()), $method->getStartLine() - 1, $method->getEndLine() - $method->getStartLine() + 1);
$signature = strstr(implode('', $lines), '{', true);

return (bool) preg_match('/(\?|null\s*\|)[\\\\\w]+\s+\$' . $parameter->getName() . '\b/', $signature);
}
}
2 changes: 1 addition & 1 deletion src/AbstractGithubObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ abstract class AbstractGithubObject
*
* @since 1.0
*/
public function __construct(Registry $options = null, BaseHttp $client = null)
public function __construct(?Registry $options = null, ?BaseHttp $client = null)
{
$this->options = $options ?: new Registry();
$this->client = $client ?: (new HttpFactory())->getHttp($this->options);
Expand Down
2 changes: 1 addition & 1 deletion src/AbstractPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class AbstractPackage extends AbstractGithubObject
*
* @since 1.0
*/
public function __construct(Registry $options = null, Http $client = null)
public function __construct(?Registry $options = null, ?Http $client = null)
{
parent::__construct($options, $client);

Expand Down
2 changes: 1 addition & 1 deletion src/Github.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Github
*
* @since 1.0
*/
public function __construct(Registry $options = null, Http $client = null)
public function __construct(?Registry $options = null, ?Http $client = null)
{
$this->options = $options ?: new Registry();

Expand Down
10 changes: 5 additions & 5 deletions src/Package/Activity/Notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Notifications extends AbstractPackage
*
* @since 1.0
*/
public function getList($all = true, $participating = true, \DateTimeInterface $since = null, \DateTimeInterface $before = null)
public function getList($all = true, $participating = true, ?\DateTimeInterface $since = null, ?\DateTimeInterface $before = null)
{
// Build the request path.
$path = '/notifications';
Expand Down Expand Up @@ -81,8 +81,8 @@ public function getListRepository(
$repo,
$all = true,
$participating = true,
\DateTimeInterface $since = null,
\DateTimeInterface $before = null
?\DateTimeInterface $since = null,
?\DateTimeInterface $before = null
) {
// Build the request path.
$path = '/repos/' . $owner . '/' . $repo . '/notifications';
Expand Down Expand Up @@ -122,7 +122,7 @@ public function getListRepository(
*
* @since 1.0
*/
public function markRead($unread = true, $read = true, \DateTimeInterface $lastReadAt = null)
public function markRead($unread = true, $read = true, ?\DateTimeInterface $lastReadAt = null)
{
// Build the request path.
$path = '/notifications';
Expand Down Expand Up @@ -158,7 +158,7 @@ public function markRead($unread = true, $read = true, \DateTimeInterface $lastR
*
* @since 1.0
*/
public function markReadRepository($owner, $repo, $unread, $read, \DateTimeInterface $lastReadAt = null)
public function markReadRepository($owner, $repo, $unread, $read, ?\DateTimeInterface $lastReadAt = null)
{
// Build the request path.
$path = '/repos/' . $owner . '/' . $repo . '/notifications';
Expand Down
6 changes: 3 additions & 3 deletions src/Package/Gists.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public function getList($page = 0, $limit = 0)
* @since 1.0
* @throws \DomainException
*/
public function getListByUser($user, $page = 0, $limit = 0, \DateTime $since = null)
public function getListByUser($user, $page = 0, $limit = 0, ?\DateTime $since = null)
{
// Build the request path.
$uri = $this->fetchUrl('/users/' . $user . '/gists', $page, $limit);
Expand All @@ -256,7 +256,7 @@ public function getListByUser($user, $page = 0, $limit = 0, \DateTime $since = n
* @since 1.0
* @throws \DomainException
*/
public function getListPublic($page = 0, $limit = 0, \DateTime $since = null)
public function getListPublic($page = 0, $limit = 0, ?\DateTime $since = null)
{
// Build the request path.
$uri = $this->fetchUrl('/gists/public', $page, $limit);
Expand All @@ -281,7 +281,7 @@ public function getListPublic($page = 0, $limit = 0, \DateTime $since = null)
* @since 1.0
* @throws \DomainException
*/
public function getListStarred($page = 0, $limit = 0, \DateTime $since = null)
public function getListStarred($page = 0, $limit = 0, ?\DateTime $since = null)
{
// Build the request path.
$uri = $this->fetchUrl('/gists/starred', $page, $limit);
Expand Down
4 changes: 2 additions & 2 deletions src/Package/Issues.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public function getList(
$labels = null,
$sort = null,
$direction = null,
\DateTimeInterface $since = null,
?\DateTimeInterface $since = null,
$page = 0,
$limit = 0
) {
Expand Down Expand Up @@ -256,7 +256,7 @@ public function getListByRepository(
$labels = null,
$sort = null,
$direction = null,
\DateTimeInterface $since = null,
?\DateTimeInterface $since = null,
$page = 0,
$limit = 0
) {
Expand Down
4 changes: 2 additions & 2 deletions src/Package/Issues/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Comments extends AbstractPackage
* @since 1.0
* @throws \DomainException
*/
public function getList($owner, $repo, $issueId, $page = 0, $limit = 0, \DateTimeInterface $since = null)
public function getList($owner, $repo, $issueId, $page = 0, $limit = 0, ?\DateTimeInterface $since = null)
{
// Build the request path.
$path = '/repos/' . $owner . '/' . $repo . '/issues/' . (int) $issueId . '/comments';
Expand Down Expand Up @@ -68,7 +68,7 @@ public function getList($owner, $repo, $issueId, $page = 0, $limit = 0, \DateTim
* @throws \UnexpectedValueException
* @throws \DomainException
*/
public function getRepositoryList($owner, $repo, $sort = 'created', $direction = 'asc', \DateTimeInterface $since = null)
public function getRepositoryList($owner, $repo, $sort = 'created', $direction = 'asc', ?\DateTimeInterface $since = null)
{
// Build the request path.
$path = '/repos/' . $owner . '/' . $repo . '/issues/comments';
Expand Down
2 changes: 1 addition & 1 deletion src/Package/Repositories/Commits.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Commits extends AbstractPackage
* @since 1.0
* @throws \DomainException
*/
public function getList($user, $repo, $sha = '', $path = '', $author = '', \DateTimeInterface $since = null, \DateTimeInterface $until = null)
public function getList($user, $repo, $sha = '', $path = '', $author = '', ?\DateTimeInterface $since = null, ?\DateTimeInterface $until = null)
{
// Build the request path.
$rPath = '/repos/' . $user . '/' . $repo . '/commits';
Expand Down
Loading