[TOOLS] Fix errors reported by PHPStan

This commit is contained in:
Hugo Sales 2022-02-26 14:09:41 +00:00
parent 8c15d21591
commit 5188a473d0
Signed by untrusted user: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
12 changed files with 82 additions and 49 deletions

View File

@ -57,8 +57,8 @@ class Circle extends Component
{ {
use MetaCollectionTrait; use MetaCollectionTrait;
public const TAG_CIRCLE_REGEX = '/' . Nickname::BEFORE_MENTIONS . '@#([\pL\pN_\-\.]{1,64})/'; public const TAG_CIRCLE_REGEX = '/' . Nickname::BEFORE_MENTIONS . '@#([\pL\pN_\-\.]{1,64})/';
protected string $slug = 'circle'; protected const SLUG = 'circle';
protected string $plural_slug = 'circles'; protected const PLURAL_SLUG = 'circles';
public function onAddRoute(RouteLoader $r): bool public function onAddRoute(RouteLoader $r): bool
{ {

View File

@ -33,9 +33,9 @@ use Component\Collection\Util\Controller\MetaCollectionController;
class Circles extends MetaCollectionController class Circles extends MetaCollectionController
{ {
protected string $slug = 'circle'; protected const SLUG = 'circle';
protected string $plural_slug = 'circles'; protected const PLURAL_SLUG = 'circles';
protected string $page_title = 'Actor circles'; protected string $page_title = 'Actor circles';
public function createCollection(int $owner_id, string $name) public function createCollection(int $owner_id, string $name)
{ {

View File

@ -43,9 +43,9 @@ use Symfony\Component\HttpFoundation\Request;
abstract class MetaCollectionController extends FeedController abstract class MetaCollectionController extends FeedController
{ {
protected string $slug = 'collectionsEntry'; protected const SLUG = 'collectionsEntry';
protected string $plural_slug = 'collectionsList'; protected const PLURAL_SLUG = 'collectionsList';
protected string $page_title = 'Collections'; protected string $page_title = 'Collections';
abstract public function getCollectionUrl(int $owner_id, string $owner_nickname, int $collection_id): string; abstract public function getCollectionUrl(int $owner_id, string $owner_nickname, int $collection_id): string;
abstract public function getCollectionItems(int $owner_id, $collection_id): array; abstract public function getCollectionItems(int $owner_id, $collection_id): array;
@ -76,8 +76,8 @@ abstract class MetaCollectionController extends FeedController
{ {
$collections = $this->getCollectionsByActorId($id); $collections = $this->getCollectionsByActorId($id);
$create_title = _m('Create a ' . mb_strtolower(preg_replace('/([a-z0-9])([A-Z])/', '$1 $2', $this->slug))); $create_title = _m('Create a ' . mb_strtolower(preg_replace('/([a-z0-9])([A-Z])/', '$1 $2', static::SLUG)));
$collections_title = _m('The ' . mb_strtolower(preg_replace('/([a-z0-9])([A-Z])/', '$1 $2', $this->plural_slug))); $collections_title = _m('The ' . mb_strtolower(preg_replace('/([a-z0-9])([A-Z])/', '$1 $2', static::PLURAL_SLUG)));
// create collection form // create collection form
$create = null; $create = null;
if (Common::user()?->getId() === $id) { if (Common::user()?->getId() === $id) {
@ -111,8 +111,8 @@ abstract class MetaCollectionController extends FeedController
// //
// Instead, I'm using an anonymous class to encapsulate // Instead, I'm using an anonymous class to encapsulate
// the functions and passing that class to the template. // the functions and passing that class to the template.
// This is suggested at https://stackoverflow.com/a/50364502. // This is suggested at https://web.archive.org/web/20220226132328/https://stackoverflow.com/questions/3595727/twig-pass-function-into-template/50364502
$fn = new class($id, $nickname, $request, $this, $this->slug) { $fn = new class($id, $nickname, $request, $this, static::SLUG) {
private $id; private $id;
private $nick; private $nick;
private $request; private $request;

View File

@ -46,8 +46,8 @@ use Symfony\Component\HttpFoundation\Request;
trait MetaCollectionTrait trait MetaCollectionTrait
{ {
//protected string $slug = 'collection'; //protected const SLUG = 'collection';
//protected string $plural_slug = 'collections'; //protected const PLURAL_SLUG = 'collections';
/** /**
* create a collection owned by Actor $owner. * create a collection owned by Actor $owner.
@ -127,9 +127,9 @@ trait MetaCollectionTrait
}, },
]], ]],
['add', SubmitType::class, [ ['add', SubmitType::class, [
'label' => _m('Add to ' . $this->plural_slug), 'label' => _m('Add to ' . static::PLURAL_SLUG),
'attr' => [ 'attr' => [
'title' => _m('Add to ' . $this->plural_slug), 'title' => _m('Add to ' . static::PLURAL_SLUG),
], ],
]], ]],
]); ]);
@ -151,17 +151,17 @@ trait MetaCollectionTrait
// form: add to new collection // form: add to new collection
$create_form = Form::create([ $create_form = Form::create([
['name', TextType::class, [ ['name', TextType::class, [
'label' => _m('Add to a new ' . $this->slug), 'label' => _m('Add to a new ' . static::SLUG),
'attr' => [ 'attr' => [
'placeholder' => _m('New ' . $this->slug . ' name'), 'placeholder' => _m('New ' . static::SLUG . ' name'),
'required' => 'required', 'required' => 'required',
], ],
'data' => '', 'data' => '',
]], ]],
['create', SubmitType::class, [ ['create', SubmitType::class, [
'label' => _m('Create a new ' . $this->slug), 'label' => _m('Create a new ' . static::SLUG),
'attr' => [ 'attr' => [
'title' => _m('Create a new ' . $this->slug), 'title' => _m('Create a new ' . static::SLUG),
], ],
]], ]],
]); ]);
@ -176,7 +176,7 @@ trait MetaCollectionTrait
$res[] = Formatting::twigRenderFile( $res[] = Formatting::twigRenderFile(
'collection/widget_add_to.html.twig', 'collection/widget_add_to.html.twig',
[ [
'ctitle' => _m('Add to ' . $this->plural_slug), 'ctitle' => _m('Add to ' . static::PLURAL_SLUG),
'user' => $user, 'user' => $user,
'has_collections' => \count($collections) > 0, 'has_collections' => \count($collections) > 0,
'add_form' => $add_form->createView(), 'add_form' => $add_form->createView(),

View File

@ -50,8 +50,9 @@ abstract class Parser
* recognises either spaces (currently `or`, should be fuzzy match), `OR` or `|` (`or`) and `AND` or `&` (`and`) * recognises either spaces (currently `or`, should be fuzzy match), `OR` or `|` (`or`) and `AND` or `&` (`and`)
* *
* TODO: Better fuzzy match, implement exact match with quotes and nesting with parens * TODO: Better fuzzy match, implement exact match with quotes and nesting with parens
* TODO: Proper parser, tokenize better. Mostly a rewrite
* *
* @return Criteria[] * @return array{?Criteria, ?Criteria} [?$note_criteria, ?$actor_criteria]
*/ */
public static function parse(string $input, ?string $locale = null, ?Actor $actor = null, int $level = 0): array public static function parse(string $input, ?string $locale = null, ?Actor $actor = null, int $level = 0): array
{ {
@ -83,13 +84,13 @@ abstract class Parser
//throw new ServerException("No one claimed responsibility for a match term: {$term}"); //throw new ServerException("No one claimed responsibility for a match term: {$term}");
// It's okay if the term doesn't exist, just perform a regular search // It's okay if the term doesn't exist, just perform a regular search
} }
if (!empty($note_res)) { // @phpstan-ignore-line if (!empty($note_res)) { // @phpstan-ignore-line currently an open bug. See https://web.archive.org/web/20220226131651/https://github.com/phpstan/phpstan/issues/6234
if (\is_array($note_res)) { if (\is_array($note_res)) {
$note_res = $eb->orX(...$note_res); $note_res = $eb->orX(...$note_res);
} }
$note_parts[] = $note_res; $note_parts[] = $note_res;
} }
if (!empty($actor_res)) { if (!empty($actor_res)) { // @phpstan-ignore-line currently an open bug. See https://web.archive.org/web/20220226131651/https://github.com/phpstan/phpstan/issues/6234
if (\is_array($actor_res)) { if (\is_array($actor_res)) {
$actor_res = $eb->orX(...$actor_res); $actor_res = $eb->orX(...$actor_res);
} }
@ -108,18 +109,18 @@ abstract class Parser
} }
} }
// TODO // TODO
if (!$match) { // @phpstan-ignore-line if (!$match) {
++$right; ++$right;
} }
} }
$note_criteria = null; $note_criteria = null;
$actor_criteria = null; $actor_criteria = null;
if (!empty($note_parts)) { // @phpstan-ignore-line if (!empty($note_parts)) {
self::connectParts($note_parts, $note_criteria_arr, $last_op, $eb, force: true); self::connectParts($note_parts, $note_criteria_arr, $last_op, $eb, force: true);
$note_criteria = new Criteria($eb->orX(...$note_criteria_arr)); $note_criteria = new Criteria($eb->orX(...$note_criteria_arr));
} }
if (!empty($actor_parts)) { // @phpstan-ignore-line if (!empty($actor_parts)) { // @phpstan-ignore-line weird, but this whole thing needs a rewrite
self::connectParts($actor_parts, $actor_criteria_arr, $last_op, $eb, force: true); self::connectParts($actor_parts, $actor_criteria_arr, $last_op, $eb, force: true);
$actor_criteria = new Criteria($eb->orX(...$actor_criteria_arr)); $actor_criteria = new Criteria($eb->orX(...$actor_criteria_arr));
} }

View File

@ -121,7 +121,7 @@ class Posting extends Component
_m('Local') => VisibilityScope::LOCAL->value, _m('Local') => VisibilityScope::LOCAL->value,
_m('Addressee') => VisibilityScope::ADDRESSEE->value, _m('Addressee') => VisibilityScope::ADDRESSEE->value,
]; ];
if (!\is_null($context_actor) && $context_actor->isGroup()) { if (!\is_null($context_actor) && $context_actor->isGroup()) { // @phpstan-ignore-line currently an open bug. See https://web.archive.org/web/20220226131651/https://github.com/phpstan/phpstan/issues/6234
if ($actor->canModerate($context_actor)) { if ($actor->canModerate($context_actor)) {
if ($context_actor->getRoles() & ActorLocalRoles::PRIVATE_GROUP) { if ($context_actor->getRoles() & ActorLocalRoles::PRIVATE_GROUP) {
$visibility_options = array_merge([_m('Group') => VisibilityScope::GROUP->value], $visibility_options); $visibility_options = array_merge([_m('Group') => VisibilityScope::GROUP->value], $visibility_options);

View File

@ -50,8 +50,8 @@ use Symfony\Component\HttpFoundation\Request;
class AttachmentCollections extends Plugin class AttachmentCollections extends Plugin
{ {
use MetaCollectionTrait; use MetaCollectionTrait;
protected string $slug = 'collection'; protected const SLUG = 'collection';
protected string $plural_slug = 'collections'; protected const PLURAL_SLUG = 'collections';
protected function createCollection(Actor $owner, array $vars, string $name) protected function createCollection(Actor $owner, array $vars, string $name)
{ {
$col = AttachmentCollection::create([ $col = AttachmentCollection::create([

View File

@ -33,6 +33,8 @@ use Symfony\Component\HttpFoundation\Request;
class Bundles extends Plugin class Bundles extends Plugin
{ {
use MetaCollectionTrait; use MetaCollectionTrait;
protected const SLUG = 'bundle';
protected const PLURAL_SLUG = 'bundles';
protected function createCollection(Actor $owner, array $vars, string $name) protected function createCollection(Actor $owner, array $vars, string $name)
{ {

View File

@ -33,14 +33,43 @@ namespace Plugin\OAuth2\Util;
use App\Core\Entity; use App\Core\Entity;
use DateTimeImmutable; use DateTimeImmutable;
use DateTimeInterface;
use Functional as F; use Functional as F;
use League\OAuth2\Server\Entities\ClientEntityInterface; use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Entities\ScopeEntityInterface; use League\OAuth2\Server\Entities\ScopeEntityInterface;
use League\OAuth2\Server\Entities\TokenInterface; use League\OAuth2\Server\Entities\TokenInterface;
use Plugin\OAuth2\Repository; use Plugin\OAuth2\Repository;
/**
* A type of token, needs to be extended.
*
* Since there's no way to specify an abstract method that returns a
* child of self, need to use method annotations
*
* @template T of self
*
* @method T setId(string $id)
* @method T setExpiry(\DateTimeInterface $expiry)
* @method T setUserId(?int $id)
* @method T setClientId(string $id)
* @method T setTokenScopes(string $scopes)
*
* From Entity:
* @method bool hasTokenScopes()
*/
abstract class Token extends Entity implements TokenInterface abstract class Token extends Entity implements TokenInterface
{ {
abstract public function getId(): string;
// abstract public function setId(string $id): child;
abstract public function getExpiry(): DateTimeInterface;
// abstract public function setExpiry(\DateTimeInterface $expiry): child;
abstract public function getUserId(): ?int;
// abstract public function setUserId(?int $id): child;
abstract public function getClientId(): string;
// abstract public function setClientId(string $id): child;
abstract public function getTokenScopes(): string;
// abstract public function setTokenScopes(string $scopes): child;
public function getIdentifier(): string public function getIdentifier(): string
{ {
return $this->getId(); return $this->getId();
@ -56,7 +85,7 @@ abstract class Token extends Entity implements TokenInterface
*/ */
public function getExpiryDateTime(): DateTimeImmutable public function getExpiryDateTime(): DateTimeImmutable
{ {
return $this->getExpiry(); return DateTimeImmutable::createFromInterface($this->getExpiry());
} }
/** /**

View File

@ -248,7 +248,7 @@ abstract class Controller extends AbstractController implements EventSubscriberI
'int' => $this->request->query->getInt($args[0]), 'int' => $this->request->query->getInt($args[0]),
'bool' => $this->request->query->getBoolean($args[0]), 'bool' => $this->request->query->getBoolean($args[0]),
'string' => $this->request->query->get($args[0]), 'string' => $this->request->query->get($args[0]),
default => throw new BugFoundException('Inconsistent switch/match spotted'), default => throw new BugFoundException('Inconsistent switch/match spotted'), // @phpstan-ignore-line
}; };
} }

View File

@ -56,6 +56,11 @@ use Functional as F;
* @author Hugo Sales <hugo@hsal.es> * @author Hugo Sales <hugo@hsal.es>
* @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*
*
* @method bool isPerson()
* @method bool isGroup()
* @method bool isBot()
*/ */
class Actor extends Entity class Actor extends Entity
{ {
@ -533,11 +538,6 @@ class Actor extends Entity
} }
} }
/**
* @method bool isPerson()
* @method bool isGroup()
* @method bool isBot()
*/
public function __call(string $name, array $arguments): mixed public function __call(string $name, array $arguments): mixed
{ {
if (Formatting::startsWith($name, 'is')) { if (Formatting::startsWith($name, 'is')) {

View File

@ -21,6 +21,7 @@ declare(strict_types = 1);
namespace App\Tests\Util; namespace App\Tests\Util;
use App\Util\HTML;
use Jchook\AssertThrows\AssertThrows; use Jchook\AssertThrows\AssertThrows;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use TypeError; use TypeError;
@ -31,17 +32,17 @@ class HTMLTest extends WebTestCase
public function testHTML() public function testHTML()
{ {
static::assertSame('', HTML\HTML::html('')); static::assertSame('', HTML::html(''));
static::assertSame('<a></a>', HTML\HTML::html(['a' => ''])); static::assertSame('<a></a>', HTML::html(['a' => '']));
static::assertSame("<div>\n <p></p>\n</div>", HTML\HTML::html(['div' => ['p' => '']])); static::assertSame("<div>\n <p></p>\n</div>", HTML::html(['div' => ['p' => '']]));
static::assertSame("<div>\n <div>\n <p></p>\n </div>\n</div>", HTML\HTML::html(['div' => ['div' => ['p' => '']]])); static::assertSame("<div>\n <div>\n <p></p>\n </div>\n</div>", HTML::html(['div' => ['div' => ['p' => '']]]));
static::assertSame("<div>\n <div>\n <div>\n <p></p>\n </div>\n </div>\n</div>", HTML\HTML::html(['div' => ['div' => ['div' => ['p' => '']]]])); static::assertSame("<div>\n <div>\n <div>\n <p></p>\n </div>\n </div>\n</div>", HTML::html(['div' => ['div' => ['div' => ['p' => '']]]]));
static::assertSame('<a href="test"><p></p></a>', HTML\HTML::html(['a' => ['attrs' => ['href' => 'test'], 'p' => '']])); static::assertSame('<a href="test"><p></p></a>', HTML::html(['a' => ['attrs' => ['href' => 'test'], 'p' => '']]));
static::assertSame('<a><p>foo</p><br></a>', HTML\HTML::html(['a' => ['p' => 'foo', 'br' => 'empty']])); static::assertSame('<a><p>foo</p><br></a>', HTML::html(['a' => ['p' => 'foo', 'br' => 'empty']]));
static::assertSame("<div>\n <a><p>foo</p><br></a>\n</div>", HTML\HTML::html(['div' => ['a' => ['p' => 'foo', 'br' => 'empty']]])); static::assertSame("<div>\n <a><p>foo</p><br></a>\n</div>", HTML::html(['div' => ['a' => ['p' => 'foo', 'br' => 'empty']]]));
static::assertSame('<div><a><p>foo</p><br></a></div>', HTML\HTML::html(['div' => ['a' => ['p' => 'foo', 'br' => 'empty']]], options: ['indent' => false])); static::assertSame('<div><a><p>foo</p><br></a></div>', HTML::html(['div' => ['a' => ['p' => 'foo', 'br' => 'empty']]], options: ['indent' => false]));
static::assertThrows(TypeError::class, fn () => HTML\HTML::html(1)); static::assertThrows(TypeError::class, fn () => HTML::html(1));
static::assertSame('<a href="test">foo</a>', HTML\HTML::tag('a', ['href' => 'test'], content: 'foo', options: ['empty' => false])); static::assertSame('<a href="test">foo</a>', HTML::tag('a', ['href' => 'test'], content: 'foo', options: ['empty' => false]));
static::assertSame('<br>', HTML\HTML::tag('br', attrs: null, content: null, options: ['empty' => true])); static::assertSame('<br>', HTML::tag('br', attrs: null, content: null, options: ['empty' => true]));
} }
} }