A free, self-hostable news aggregator…
PHPStan Level 6 for FreshRSS_Share (#5289)
Alexandre Alapetitea13a20de2070
diff --git a/app/Models/Share.php b/app/Models/Share.phpindex 375b17f01..7bd6de9cf 100644--- a/app/Models/Share.php+++ b/app/Models/Share.php@@ -6,28 +6,31 @@class FreshRSS_Share {/*** The list of available sharing options.+ * @var array<string,FreshRSS_Share>*/- private static $list_sharing = array();+ private static $list_sharing = [];/*** Register a new sharing option.- * @param array<string,string|array<string>> $share_options is an array defining the share option.+ * @param array{'type':string,'url':string,'transform'?:array<string>|array<string,string>,'field'?:string,'help'?:string,'form'?:'simple'|'advanced',+ * 'method'?:'GET'|'POST','HTMLtag'?:'button','deprecated'?:bool} $share_options is an array defining the share option.*/- public static function register($share_options) {+ private static function register(array $share_options): void {$type = $share_options['type'];-if (isset(self::$list_sharing[$type])) {return;}- $HTMLtag = isset($share_options['HTMLtag']) ? $share_options['HTMLtag'] : 'link';- $isDeprecated = isset($share_options['deprecated']) ? $share_options['deprecated'] : false;- $help_url = isset($share_options['help']) ? $share_options['help'] : '';- $field = isset($share_options['field']) ? $share_options['field'] : null;self::$list_sharing[$type] = new FreshRSS_Share(- $type, $share_options['url'], $share_options['transform'],- $share_options['form'], $help_url, $share_options['method'],- $field, $HTMLtag, $isDeprecated+ $type,+ $share_options['url'],+ $share_options['transform'] ?? [],+ $share_options['form'] ?? 'simple',+ $share_options['help'] ?? '',+ $share_options['method'] ?? 'GET',+ $share_options['field'] ?? null,+ $share_options['HTMLtag'] ?? null,+ $share_options['deprecated'] ?? false);}@@ -35,7 +38,7 @@ class FreshRSS_Share {* Register sharing options in a file.* @param string $filename the name of the file to load.*/- public static function load($filename) {+ public static function load(string $filename): void {$shares_from_file = @include($filename);if (!is_array($shares_from_file)) {$shares_from_file = array();@@ -53,17 +56,17 @@ class FreshRSS_Share {/*** Return the list of sharing options.- * @return array<FreshRSS_Share>+ * @return array<string,FreshRSS_Share>*/- public static function enum() {+ public static function enum(): array {return self::$list_sharing;}/**- * Return FreshRSS_Share object related to the given type.* @param string $type the share type, null if $type is not registered.+ * @return FreshRSS_Share|null object related to the given type.*/- public static function get($type) {+ public static function get(string $type): ?FreshRSS_Share {if (!isset(self::$list_sharing[$type])) {return null;}@@ -71,48 +74,68 @@ class FreshRSS_Share {return self::$list_sharing[$type];}- /**- *- */++ /** @var string */private $type = '';+ /** @var string */private $name = '';+ /** @var string */private $url_transform = '';- private $transform = array();+ /** @var array<string>|array<string,array<string>> */+ private $transforms = [];+ /**+ * @phpstan-var 'simple'|'advanced'+ * @var string+ */private $form_type = 'simple';+ /** @var string */private $help_url = '';+ /** @var string|null */private $custom_name = null;+ /** @var string|null */private $base_url = null;+ /** @var string|null */private $id = null;+ /** @var string|null */private $title = null;+ /** @var string|null */private $link = null;+ /** @var bool */private $isDeprecated = false;+ /**+ * @phpstan-var 'GET'|'POST'+ * @var string+ */private $method = 'GET';+ /** @var string|null */private $field;- private $HTMLtag = 'link';+ /**+ * @phpstan-var 'button'|null+ * @var string+ */+ private $HTMLtag;/*** Create a FreshRSS_Share object.* @param string $type is a unique string defining the kind of share option.* @param string $url_transform defines the url format to use in order to share.- * @param array<string> $transform is an array of transformations to apply on link and title.- * @param string $form_type defines which form we have to use to complete. "simple"+ * @param array<string>|array<string,array<string>> $transforms is an array of transformations to apply on link and title.+ * @param 'simple'|'advanced' $form_type defines which form we have to use to complete. "simple"* is typically for a centralized service while "advanced" is for* decentralized ones.* @param string $help_url is an optional url to give help on this option.- * @param string $method defines the sharing method (GET or POST)+ * @param 'GET'|'POST' $method defines the sharing method (GET or POST)+ * @param 'button'|null $HTMLtag*/- private function __construct($type, $url_transform, $transform, $form_type, $help_url, $method, $field, $HTMLtag, $isDeprecated = false) {+ private function __construct(string $type, string $url_transform, array $transforms, string $form_type,+ string $help_url, string $method, ?string $field, ?string $HTMLtag, bool $isDeprecated = false) {$this->type = $type;$this->name = _t('gen.share.' . $type);$this->url_transform = $url_transform;$this->help_url = $help_url;$this->HTMLtag = $HTMLtag;$this->isDeprecated = $isDeprecated;-- if (!is_array($transform)) {- $transform = array();- }- $this->transform = $transform;+ $this->transforms = $transforms;if (!in_array($form_type, array('simple', 'advanced'))) {$form_type = 'simple';@@ -130,7 +153,7 @@ class FreshRSS_Share {* @param array<string,string> $options is a list of information to update where keys should be* in this list: name, url, id, title, link.*/- public function update($options) {+ public function update(array $options): void {$available_options = array('name' => 'custom_name','url' => 'base_url',@@ -151,14 +174,15 @@ class FreshRSS_Share {/*** Return the current type of the share option.*/- public function type() {+ public function type(): string {return $this->type;}/*** Return the current method of the share option.+ * @return 'GET'|'POST'*/- public function method() {+ public function method(): string {return $this->method;}@@ -166,35 +190,37 @@ class FreshRSS_Share {* Return the current field of the share option. It’s null for shares* using the GET method.*/- public function field() {+ public function field(): ?string {return $this->field;}/*** Return the current form type of the share option.+ * @return 'simple'|'advanced'*/- public function formType() {+ public function formType(): string {return $this->form_type;}/*** Return the current help url of the share option.*/- public function help() {+ public function help(): string {return $this->help_url;}/**- * Return the type of HTML tag of the share option.+ * Return the custom type of HTML tag of the share option, null for default.+ * @return 'button'|null*/- public function HTMLtag() {+ public function HTMLtag(): ?string {return $this->HTMLtag;}/*** Return the current name of the share option.*/- public function name($real = false) {+ public function name(bool $real = false): ?string {if ($real || is_null($this->custom_name) || empty($this->custom_name)) {return $this->name;} else {@@ -205,21 +231,21 @@ class FreshRSS_Share {/*** Return the current base url of the share option.*/- public function baseUrl() {+ public function baseUrl(): string {return $this->base_url;}/*** Return the deprecated status of the share option.*/- public function isDeprecated() {+ public function isDeprecated(): bool {return $this->isDeprecated;}/*** Return the current url by merging url_transform and base_url.*/- public function url() {+ public function url(): string {$matches = array('~ID~','~URL~',@@ -239,45 +265,45 @@ class FreshRSS_Share {* Return the id.* @param bool $raw true if we should get the id without transformations.*/- public function id($raw = false) {+ public function id(bool $raw = false): ?string {if ($raw) {return $this->id;}- return $this->transform($this->id, $this->getTransform('id'));+ return self::transform($this->id, $this->getTransform('id'));}/*** Return the title.* @param bool $raw true if we should get the title without transformations.*/- public function title($raw = false) {+ public function title(bool $raw = false): string {if ($raw) {return $this->title;}- return $this->transform($this->title, $this->getTransform('title'));+ return self::transform($this->title, $this->getTransform('title'));}/*** Return the link.* @param bool $raw true if we should get the link without transformations.*/- public function link($raw = false) {+ public function link(bool $raw = false): string {if ($raw) {return $this->link;}- return $this->transform($this->link, $this->getTransform('link'));+ return self::transform($this->link, $this->getTransform('link'));}/*** Transform a data with the given functions.* @param string $data the data to transform.* @param array<string> $transform an array containing a list of functions to apply.- * @return mixed the transformed data.+ * @return string the transformed data.*/- private static function transform($data, $transform) {+ private static function transform(string $data, $transform): string {if (!is_array($transform) || empty($transform)) {return $data;}@@ -296,11 +322,11 @@ class FreshRSS_Share {* @param string $attr the attribute of which we want the transformations.* @return array<string> containing a list of transformations to apply.*/- private function getTransform($attr) {- if (array_key_exists($attr, $this->transform)) {- return $this->transform[$attr];+ private function getTransform(string $attr): array {+ if (array_key_exists($attr, $this->transforms)) {+ return $this->transforms[$attr];}- return $this->transform;+ return $this->transforms;}}diff --git a/app/shares.php b/app/shares.phpindex e64503f32..6feb13898 100644--- a/app/shares.php+++ b/app/shares.php@@ -27,208 +27,208 @@* - 'method' is the HTTP method (POST or GET) used to share a link.*/-return array(- 'archiveORG' => array(+return [+ 'archiveORG' => ['url' => 'https://web.archive.org/save/~LINK~',- 'transform' => array(),+ 'transform' => [],'help' => 'https://web.archive.org','form' => 'simple','method' => 'GET',- ),- 'archivePH' => array(+ ],+ 'archivePH' => ['url' => 'https://archive.ph/submit/?url=~LINK~',- 'transform' => array(),+ 'transform' => [],'help' => 'https://archive.ph/','form' => 'simple','method' => 'GET',- ),- 'blogotext' => array(+ ],+ 'blogotext' => ['deprecated' => true,'url' => '~URL~/admin/links.php?url=~LINK~',- 'transform' => array(),+ 'transform' => [],'help' => 'http://lehollandaisvolant.net/blogotext/fr/','form' => 'advanced','method' => 'GET',- ),- 'buffer' => array(+ ],+ 'buffer' => ['url' => 'https://publish.buffer.com/compose?url=~LINK~&text=~TITLE~',- 'transform' => array('rawurlencode'),+ 'transform' => ['rawurlencode'],'help' => 'https://support.buffer.com/hc/en-us/articles/360035587394-Scheduling-posts','form' => 'simple','method' => 'GET',- ),- 'clipboard' => array(+ ],+ 'clipboard' => ['HTMLtag' => 'button','url' => '~LINK~',- 'transform' => array(),+ 'transform' => [],'form' => 'simple','method' => 'GET',- ),- 'diaspora' => array(+ ],+ 'diaspora' => ['url' => '~URL~/bookmarklet?url=~LINK~&title=~TITLE~',- 'transform' => array('rawurlencode'),+ 'transform' => ['rawurlencode'],'help' => 'https://diasporafoundation.org/','form' => 'advanced','method' => 'GET',- ),- 'email' => array(+ ],+ 'email' => ['url' => 'mailto:?subject=~TITLE~&body=~LINK~',- 'transform' => array('rawurlencode'),+ 'transform' => ['rawurlencode'],'form' => 'simple','method' => 'GET',- ),- 'email-webmail-firefox-fix' => array( // see https://github.com/FreshRSS/FreshRSS/issues/2666+ ],+ 'email-webmail-firefox-fix' => [ // see https://github.com/FreshRSS/FreshRSS/issues/2666'url' => 'mailto:?subject=~TITLE~&body=~LINK~',- 'transform' => array('rawurlencode'),+ 'transform' => ['rawurlencode'],'form' => 'simple','method' => 'GET',- ),- 'facebook' => array(+ ],+ 'facebook' => ['url' => 'https://www.facebook.com/sharer.php?u=~LINK~&t=~TITLE~',- 'transform' => array('rawurlencode'),+ 'transform' => ['rawurlencode'],'form' => 'simple','method' => 'GET',- ),- 'gnusocial' => array(+ ],+ 'gnusocial' => ['url' => '~URL~/notice/new?content=~TITLE~%20~LINK~',- 'transform' => array('urlencode'),+ 'transform' => ['urlencode'],'help' => 'https://gnu.io/social/','form' => 'advanced','method' => 'GET',- ),- 'jdh' => array(+ ],+ 'jdh' => ['url' => 'https://www.journalduhacker.net/stories/new?url=~LINK~&title=~TITLE~',- 'transform' => array('rawurlencode'),+ 'transform' => ['rawurlencode'],'form' => 'simple','method' => 'GET',- ),- 'Known' => array(+ ],+ 'Known' => ['url' => '~URL~/share?share_url=~LINK~&share_title=~TITLE~',- 'transform' => array('rawurlencode'),+ 'transform' => ['rawurlencode'],'help' => 'https://withknown.com/','form' => 'advanced','method' => 'GET',- ),- 'lemmy' => array(+ ],+ 'lemmy' => ['url' => '~URL~/create_post?url=~LINK~&title=~TITLE~',- 'transform' => array('rawurlencode'),+ 'transform' => ['rawurlencode'],'help' => 'https://join-lemmy.org/','form' => 'advanced','method' => 'GET',- ),- 'linkding' => array(+ ],+ 'linkding' => ['url' => '~URL~/bookmarks/new?url=~LINK~&auto_close',- 'transform' => array('rawurlencode'),+ 'transform' => ['rawurlencode'],'help' => 'https://github.com/sissbruecker/linkding/blob/master/docs/how-to.md','form' => 'advanced','method' => 'GET',- ),- 'linkedin' => array(+ ],+ 'linkedin' => ['url' => 'https://www.linkedin.com/shareArticle?url=~LINK~&title=~TITLE~&source=FreshRSS',- 'transform' => array('rawurlencode'),+ 'transform' => ['rawurlencode'],'form' => 'simple','method' => 'GET',- ),- 'mastodon' => array(+ ],+ 'mastodon' => ['url' => '~URL~/share?title=~TITLE~&url=~LINK~',- 'transform' => array('rawurlencode'),+ 'transform' => ['rawurlencode'],'help' => 'https://joinmastodon.org/','form' => 'advanced','method' => 'GET',- ),- 'movim' => array(+ ],+ 'movim' => ['url' => '~URL~/?share/~LINK~',- 'transform' => array('urlencode'),+ 'transform' => ['urlencode'],'help' => 'https://movim.eu/','form' => 'advanced','method' => 'GET',- ),- 'pinboard' => array(+ ],+ 'pinboard' => ['url' => 'https://pinboard.in/add?next=same&url=~LINK~&title=~TITLE~',- 'transform' => array('urlencode'),+ 'transform' => ['urlencode'],'help' => 'https://pinboard.in/api/','form' => 'simple','method' => 'GET',- ),- 'pinterest' => array(+ ],+ 'pinterest' => ['url' => 'https://pinterest.com/pin/create/button/?url=~LINK~',- 'transform' => array('rawurlencode'),+ 'transform' => ['rawurlencode'],'help' => 'https://pinterest.com/','form' => 'simple','method' => 'GET',- ),- 'pocket' => array(+ ],+ 'pocket' => ['url' => 'https://getpocket.com/save?url=~LINK~&title=~TITLE~',- 'transform' => array('rawurlencode'),+ 'transform' => ['rawurlencode'],'form' => 'simple','method' => 'GET',- ),- 'print' => array(+ ],+ 'print' => ['HTMLtag' => 'button','url' => '#',- 'transform' => array(),+ 'transform' => [],'form' => 'simple','method' => 'GET',- ),- 'raindrop' => array(+ ],+ 'raindrop' => ['url' => 'https://app.raindrop.io/add?link=~LINK~&title=~TITLE~',- 'transform' => array('rawurlencode'),+ 'transform' => ['rawurlencode'],'form' => 'simple','method' => 'GET',- ),- 'reddit' => array(+ ],+ 'reddit' => ['url' => 'https://www.reddit.com/submit?url=~LINK~',- 'transform' => array('rawurlencode'),+ 'transform' => ['rawurlencode'],'help' => 'https://www.reddit.com/wiki/submitting?v=c2ae883a-04b9-11e4-a68c-12313b01a1fc','form' => 'simple','method' => 'GET',- ),- 'shaarli' => array(+ ],+ 'shaarli' => ['url' => '~URL~?post=~LINK~&title=~TITLE~&source=FreshRSS',- 'transform' => array('rawurlencode'),+ 'transform' => ['rawurlencode'],'help' => 'http://sebsauvage.net/wiki/doku.php?id=php:shaarli','form' => 'advanced','method' => 'GET',- ),- 'twitter' => array(+ ],+ 'twitter' => ['url' => 'https://twitter.com/share?url=~LINK~&text=~TITLE~',- 'transform' => array('rawurlencode'),+ 'transform' => ['rawurlencode'],'form' => 'simple','method' => 'GET',- ),- 'wallabag' => array(+ ],+ 'wallabag' => ['url' => '~URL~?action=add&url=~LINK~',- 'transform' => array('rawurlencode'),+ 'transform' => ['rawurlencode'],'help' => 'http://www.wallabag.org/','form' => 'advanced','method' => 'GET',- ),- 'wallabagv2' => array(+ ],+ 'wallabagv2' => ['url' => '~URL~/bookmarklet?url=~LINK~',- 'transform' => array('rawurlencode'),+ 'transform' => ['rawurlencode'],'help' => 'http://www.wallabag.org/','form' => 'advanced','method' => 'GET',- ),- 'web-sharing-api' => array(+ ],+ 'web-sharing-api' => ['HTMLtag' => 'button','url' => '~LINK~',- 'transform' => array(),+ 'transform' => [],'form' => 'simple','method' => 'GET',- ),- 'whatsapp' => array(+ ],+ 'whatsapp' => ['url' => 'https://wa.me/?text=~TITLE~ | ~LINK~',- 'transform' => array('rawurlencode'),+ 'transform' => ['rawurlencode'],'help' => 'https://faq.whatsapp.com/iphone/how-to-link-to-whatsapp-from-a-different-app/?lang=en','form' => 'simple','method' => 'GET',- ),- 'xing' => array(+ ],+ 'xing' => ['url' => 'https://www.xing.com/spi/shares/new?url=~LINK~',- 'transform' => array('rawurlencode'),+ 'transform' => ['rawurlencode'],'help' => 'https://dev.xing.com/plugins/share_button/docs','form' => 'simple','method' => 'GET',- ),-);+ ],+];diff --git a/tests/phpstan-next.txt b/tests/phpstan-next.txtindex d6edd8746..adf62cd8c 100644--- a/tests/phpstan-next.txt+++ b/tests/phpstan-next.txt@@ -11,7 +11,6 @@./app/Models/Feed.php./app/Models/FeedDAO.php./app/Models/Search.php-./app/Models/Share.php./app/Models/TagDAO.php./app/Services/ImportService.php./cli/i18n/I18nData.php