Skip to content

Update type signatures to avoid warnings in PHP 8.1 - #172

Closed
someonewithpc wants to merge 1 commit into
getopt-php:masterfrom
someonewithpc:master
Closed

someonewithpc wants to merge 1 commit into
getopt-php:masterfrom
someonewithpc:master

Conversation

@someonewithpc

Copy link
Copy Markdown

Also fixes some errors reported by PHPStan

@tflori

tflori commented Jan 3, 2022

Copy link
Copy Markdown
Member

I'm wondering why the CI was not executed 😞 (I guess it is my fault)

have you executed tests in earlier php versions?

Comment thread src/Command.php Outdated
* @param mixed $options
*/
public function __construct($name, $handler, $options = null)
final public function __construct($name, $handler, $options = null)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that one the warnings from phpstan?

I don't want to restrict the constructor. If someone wants to overwrite the constructors - I'm fine. So why to declare it final? If you still can overwrite the constructor (even more: it means the final keyword has no effect on constructors) I would especially not add this keyword.

In conclusion I would not add final for one or the other reason:
a) they should not be final
b) it might even be ignored from php

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PHPStan emits:

Unsafe usage of new static().                                                      
See: https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static

But fair enough, I'll remove those

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I see! Thats an intresting point. Maybe we should then use self instead. but please in a different PR if you want to solve it.

anyway it sounds odd to change the constructor and not the implementation of the static create method as it is just a syntax sugar to avoid the parenthesis around the new keyword: Option::create('h')->setDescription('Show help') instead of (new Option('h'))->setDescription('Show help')

Comment thread src/GetOpt.php Outdated
* @return mixed
*/
public function offsetGet($offset)
public function offsetGet($offset): mixed

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mixed is not available in php 7 - is that a warning in php 8 now?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have to go with that backward compatible comment
https://php.watch/versions/8.1/ReturnTypeWillChange

@someonewithpc

someonewithpc commented Jan 3, 2022 •

Copy link
Copy Markdown
Author

I'm wondering why the CI was not executed disappointed (I guess it is my fault)

have you executed tests in earlier php versions?

I'm not able to run the tests in PHP 8.1, getting:

Fatal error: Cannot acquire reference to $GLOBALS in /mnt/vendor/phpunit/phpunit/src/Util/Configuration.php on line 570
Script vendor/bin/phpunit -c phpunit.xml handling the test event returned with error code 255

But they run just fine in PHP 7.4:

> vendor/bin/phpunit -c phpunit.xml
PHPUnit 7.5.20 by Sebastian Bergmann and contributors.


GetOpt\Test\Argument\ValidationTest
  ✔ (2 ms) Default Message For Option
  ✔ (0 ms) Default Message For Operand
  ✔ (0 ms) Default Message For Argument
  ✔ (0 ms) Uses Custom Message
  ✔ (0 ms) Uses Translated Descriptions
  ✔ (0 ms) Provides Value As Second Replacement
  ✔ (0 ms) Uses Callback To Get Message
  ✔ (0 ms) Provides Option And Value
  ✔ (0 ms) Provides Operand And Value

GetOpt\Test\ArgumentTest
  ✔ (0 ms) Constructor
  ✔ (0 ms) Set Default Value Not Scalar
  ✔ (0 ms) Validates
  ✔ (0 ms) Falsy Default Value

GetOpt\Test\ArgumentsTest
  ✔ (0 ms) Parse No Options
  ✔ (0 ms) Parse Unknown Option
  ✔ (0 ms) Unknown Long Option
  ✔ (0 ms) Parse Required Argument Missing
  ✔ (0 ms) Parse Multiple Options With One Hyphen
  ✔ (0 ms) Parse Cumulative Option
  ✔ (0 ms) Parse Cumulative Option Short
  ✔ (0 ms) Parse Short Option With Argument
  ✔ (0 ms) Parse Zero Argument
  ✔ (0 ms) Parse Numeric Option
  ✔ (0 ms) Parse Collapsed Short Options Required Argument Missing
  ✔ (0 ms) Parse Collapsed Short Options With Argument
  ✔ (0 ms) Parse No Argument Option And Operand
  ✔ (0 ms) Parsed Required Argument With No Space
  ✔ (0 ms) Parse Collapsed Required Argument With No Space
  ✔ (0 ms) Parse Operands Only
  ✔ (0 ms) Parse Long Option Without Argument
  ✔ (0 ms) Parse Long Option Without Argument And Operand
  ✔ (0 ms) Parse Long Option With Argument
  ✔ (0 ms) Parse Long Option With Equals Sign And Argument
  ✔ (0 ms) Parse Long Option With Value Starting With Hyphen
  ✔ (0 ms) Parse Value Starting With Hypen Required
  ✔ (0 ms) Parse No Value Starting With Hyphen Optional
  ✔ (0 ms) Parse Option With Default Value
  ✔ (0 ms) Multiple Argument Options
  ✔ (0 ms) Double Hyphen Not In Operands
  ✔ (0 ms) Single Hyphen Value
  ✔ (0 ms) Single Hyphen Operand
  ✔ (0 ms) Options After Operands
  ✔ (0 ms) Empty Operands And Options With String
  ✔ (0 ms) Empty Operands And Options With Array
  ✔ (0 ms) Space Operand
  ✔ (0 ms) Parse With Argument Validation
  ✔ (0 ms) Parse Invalid Argument
  ✔ (0 ms) String With Single Quotes
  ✔ (0 ms) String With Double Quotes
  ✔ (0 ms) Single Quotes In String
  ✔ (0 ms) Double Quotes In String
  ✔ (0 ms) Quote Concatenation
  ✔ (0 ms) Quote Escaping Double Quote
  ✔ (0 ms) Quote Escaping Single Quote
  ✔ (0 ms) Linefeed As Separator
  ✔ (0 ms) Tab As Separator
  ✔ (0 ms) Explict Arguments
  ✔ (0 ms) Using Command

GetOpt\Test\CommandTest
  ✔ (0 ms) Constructor Saves Name
  ✔ (0 ms) Names Not Allowed with data set #0
  ✔ (0 ms) Names Not Allowed with data set #1
  ✔ (0 ms) Names Not Allowed with data set #2
  ✔ (0 ms) Constructor Saves Handler
  ✔ (0 ms) Constructor Saves Options
  ✔ (0 ms) Add Options Appends Options
  ✔ (0 ms) Command With Conflicting Options Fails To Add
  ✔ (0 ms) Operands Have To Follow Commands
  ✔ (0 ms) Short Description Used For Description
  ✔ (0 ms) Description Used For Short Description
  ✔ (2 ms) Get Help For Executed Command
  ✔ (1 ms) Get Help For Commands
  ✔ (0 ms) Too Long Short Description
  ✔ (0 ms) Commands With Spaces
  ✔ (0 ms) Single Word Command Have Precedence
  ✔ (0 ms) Command Cannot Be Divided By Options

GetOpt\Test\GetoptTest
  ✔ (0 ms) Add Options
  ✔ (0 ms) Add Options Choose Short Or Long Automatically
  ✔ (0 ms) Add Options Use Default Argument Type
  ✔ (0 ms) Add Options Fails On Invalid Argument
  ✔ (0 ms) Change Mode Afterwards
  ✔ (0 ms) Add Options Fails On Conflict with data set #0
  ✔ (0 ms) Add Options Fails On Conflict with data set #1
  ✔ (0 ms) Parse Uses Global Argv When None Given
  ✔ (0 ms) Access Methods
  ✔ (0 ms) Countable
  ✔ (0 ms) Array Access
  ✔ (0 ms) Iterable
  ✔ (0 ms) Iterates Over Empty Strings
  ✔ (0 ms) Help Text With Custom Script Name
  ✔ (0 ms) Help Text With Description
  ✔ (0 ms) Throws With Invalid Parameter
  ✔ (0 ms) Add Option By String
  ✔ (0 ms) Throws For Unparsable String
  ✔ (0 ms) Throws For Invalid Parameter
  ✔ (0 ms) Isset Array Access
  ✔ (0 ms) Restircts Array Set
  ✔ (0 ms) Restricts Array Unset
  ✔ (0 ms) Add Command With Conflicting Options
  ✔ (0 ms) Get Command By Name
  ✔ (0 ms) Set Help Lang To De
  ✔ (0 ms) Returns False When File Does Not Exist

GetOpt\Test\Help\TemplateTest
  ✔ (0 ms) Renders Usage Template
  ✔ (0 ms) Renders Options Template
  ✔ (0 ms) Renders Commands Template

GetOpt\Test\MagicGettersTest
  ✔ (0 ms) Get Opt Uses Magic Getters with data set #0
  ✔ (0 ms) Get Opt Uses Magic Getters with data set #1
  ✔ (0 ms) Get Opt Uses Magic Getters with data set #2
  ✔ (0 ms) Get Opt Uses Magic Getters with data set #3
  ✔ (0 ms) Get Opt Uses Magic Getters with data set #4
  ✔ (0 ms) Get Opt Uses Magic Getters with data set #5
  ✔ (0 ms) Get Opt Uses Magic Getters with data set #6
  ✔ (0 ms) Command Uses Magic Getters with data set #0
  ✔ (0 ms) Command Uses Magic Getters with data set #1
  ✔ (0 ms) Command Uses Magic Getters with data set #2
  ✔ (0 ms) Command Uses Magic Getters with data set #3
  ✔ (0 ms) Command Uses Magic Getters with data set #4
  ✔ (0 ms) Argument Uses Magic Getters with data set #0
  ✔ (0 ms) Argument Uses Magic Getters with data set #1
  ✔ (0 ms) Operand Uses Magic Getters with data set #0
  ✔ (0 ms) Operand Uses Magic Getters with data set #1
  ✔ (0 ms) Option Uses Magic Getters with data set #0
  ✔ (0 ms) Option Uses Magic Getters with data set #1
  ✔ (0 ms) Option Uses Magic Getters with data set #2
  ✔ (0 ms) Option Uses Magic Getters with data set #3
  ✔ (0 ms) Option Uses Magic Getters with data set #4
  ✔ (0 ms) Option Uses Magic Getters with data set #5

GetOpt\Test\Operands\CommonTest
  ✔ (0 ms) Operands Are Resetted
  ✔ (0 ms) Add Operands
  ✔ (0 ms) Operand Validation
  ✔ (0 ms) Optional Operand
  ✔ (0 ms) Required Operand
  ✔ (0 ms) Get Operand By Name
  ✔ (0 ms) Default Value
  ✔ (0 ms) All Previous Operands Get Required Too
  ✔ (0 ms) Commands Can Have Operands
  ✔ (0 ms) Command With Operand
  ✔ (0 ms) Returns Null For Unknown Operands
  ✔ (0 ms) Require Makes Required
  ✔ (0 ms) Require False
  ✔ (0 ms) Require Does Not Make An Operand Multiple
  ✔ (0 ms) Multiple Makes Multiple
  ✔ (0 ms) Multiple False
  ✔ (0 ms) Required Multiple Throws Missing

GetOpt\Test\Operands\HelpTest
  ✔ (0 ms) Help Contains Operand Names
  ✔ (0 ms) Help Command Defines Operands
  ✔ (0 ms) Help Text For Multiple
  ✔ (0 ms) Help Text For Required Multiple
  ✔ (0 ms) Shows Descriptions Before Options
  ✔ (0 ms) Hides Descriptions If Requested

GetOpt\Test\Operands\MultipleTest
  ✔ (0 ms) Value For Multiple
  ✔ (0 ms) Default Value For Multiple
  ✔ (0 ms) Required Multiple
  ✔ (0 ms) Required Multiple Not To Throw
  ✔ (0 ms) Validation Of Multiple
  ✔ (0 ms) Restricts Adding After Multiple

GetOpt\Test\Operands\StrictTest
  ✔ (0 ms) No Operands Allowed
  ✔ (0 ms) Specified Operands Allowed
  ✔ (0 ms) Help Does Not Show Additional Operands

GetOpt\Test\Operands\ValueTest
  ✔ (0 ms) To String Without Value
  ✔ (0 ms) To String With Default Value
  ✔ (0 ms) To String With Value
  ✔ (0 ms) To String With Multiple Value

GetOpt\Test\OptionParserTest
  ✔ (0 ms) Parse String
  ✔ (0 ms) Parse String Empty
  ✔ (0 ms) Parse String Invalid Character
  ✔ (0 ms) Parse String Starts With Colon
  ✔ (0 ms) Parse String Triple Colon
  ✔ (0 ms) Parse Array with data set #0
  ✔ (0 ms) Parse Array with data set #1
  ✔ (0 ms) Parse Array with data set #2
  ✔ (0 ms) Parse Array Empty
  ✔ (0 ms) Parse Array Invalid

GetOpt\Test\Options\CommonTest
  ✔ (0 ms) Construct
  ✔ (0 ms) Create
  ✔ (0 ms) Construct Fails with data set #0
  ✔ (0 ms) Construct Fails with data set #1
  ✔ (0 ms) Construct Fails with data set #2
  ✔ (0 ms) Construct Fails with data set #3
  ✔ (0 ms) Construct Fails with data set #4
  ✔ (0 ms) Set Argument
  ✔ (0 ms) Set Argument Wrong Mode
  ✔ (0 ms) Set Default Value
  ✔ (0 ms) Set Validation

GetOpt\Test\Options\HelpTest
  ✔ (0 ms) Help Text
  ✔ (0 ms) Help Text Without Descriptions
  ✔ (0 ms) Help Text With Long Descriptions
  ✔ (0 ms) Long Words In Description
  ✔ (0 ms) Help Text With Argument Name
  ✔ (0 ms) Texts Get Used

GetOpt\Test\Options\NonStrictTest
  ✔ (0 ms) Additional Options Do Not Throw
  ✔ (0 ms) Stores The Argument
  ✔ (0 ms) Additional Options Are Resetted
  ✔ (0 ms) Iterates Over Additional Options
  ✔ (0 ms) Offset Exists
  ✔ (0 ms) Offset Get
  ✔ (0 ms) Stores The Count Without Value
  ✔ (0 ms) Shows Options In Usage

GetOpt\Test\Options\ValueTest
  ✔ (0 ms) Value Without Default with data set #0
  ✔ (0 ms) Value Without Default with data set #1
  ✔ (0 ms) Value Without Default with data set #2
  ✔ (0 ms) Value Without Default with data set #3
  ✔ (0 ms) Value Without Default with data set #4
  ✔ (0 ms) Value Without Default But Set Value with data set #0
  ✔ (0 ms) Value Without Default But Set Value with data set #1
  ✔ (0 ms) Value Without Default But Set Value with data set #2
  ✔ (0 ms) Value Without Default But Set Value with data set #3
  ✔ (0 ms) Value Without Default But Set Value with data set #4
  ✔ (0 ms) To String Without Argument
  ✔ (0 ms) To String With Argument
  ✔ (0 ms) To String With Multiple Arguments
  ✔ (0 ms) Default Value Not Used For Counting

GetOpt\Test\Translator\CommonTest
  ✔ (0 ms) Throws When Language Not Available
  ✔ (0 ms) Uses Translation File
  ✔ (0 ms) Uses Fall Back Translation


Time: 65 ms, Memory: 6.00 MB

OK (214 tests, 317 assertions)

@tflori

tflori commented Jan 4, 2022

Copy link
Copy Markdown
Member

indeed it is an open issue on gitlab repo: https://gitlab.com/gitlab-org/gitlab/-/issues/5667

@tflori

tflori commented Jan 4, 2022

Copy link
Copy Markdown
Member

thx for your contribution. there is another PR to fix that issue and you forgot one of the final declarations. so I will go with that one.

closed in favor of #173

@tflori tflori closed this Jan 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants