Skip to content

Replace more docblocks by type-hints #24722

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

Merged
merged 1 commit into from
Nov 7, 2017

Conversation

nicolas-grekas
Copy link
Member

@nicolas-grekas nicolas-grekas commented Oct 28, 2017

Q A
Branch? master
Bug fix? no
New feature? no
BC breaks? yes (but only on constructors and final methods)
Deprecations? no
Tests pass? yes
Fixed tickets -
License MIT
Doc PR -

@nicolas-grekas
Copy link
Member Author

Status: needs work

To be rebase on top of #24723 #24724 #24725 #24726, and failure investigated.

@kaznovac
Copy link
Contributor

good initiative, but the parameter documentation should be preserved IMHO

@nicolas-grekas nicolas-grekas force-pushed the final-types branch 3 times, most recently from 768004a to 55854e7 Compare November 5, 2017 20:24
{
try {
return $this->headers->getDate('Expires');
} catch (\RuntimeException $e) {
// according to RFC 2616 invalid date formats (e.g. "0" and "-1") must be treated as in the past
return \DateTime::createFromFormat(DATE_RFC2822, 'Sat, 01 Jan 00 00:00:00 +0000');
return \DateTime::createFromFormat('U', time() - 172800);
Copy link
Member Author

Choose a reason for hiding this comment

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

This change is required to prevent an integer overflow on 32bit systems.

@nicolas-grekas nicolas-grekas force-pushed the final-types branch 2 times, most recently from c27f96a to 342ae44 Compare November 6, 2017 15:02
@nicolas-grekas
Copy link
Member Author

nicolas-grekas commented Nov 6, 2017

Status: needs review (enjoy ;-))

Docblocks are really different than actual type hints, their respective accuracies have nothing in common.

@stof
Copy link
Member

stof commented Nov 7, 2017

We should of course use them from the start for any new API added in 4.1+

@nicolas-grekas nicolas-grekas merged commit aaf2265 into symfony:master Nov 7, 2017
nicolas-grekas added a commit that referenced this pull request Nov 7, 2017
This PR was merged into the 4.0-dev branch.

Discussion
----------

Replace more docblocks by type-hints

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | yes (but only on constructors and final methods)
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Commits
-------

aaf2265 Replace more docblocks by type-hints
@nicolas-grekas nicolas-grekas deleted the final-types branch November 7, 2017 14:55
*/
private function updateValidatorMappingFiles(ContainerBuilder $container, $mapping, $extension)
private function updateValidatorMappingFiles(ContainerBuilder $container, string $mapping, string $extension)
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't this have void return typehint?

Copy link
Member Author

Choose a reason for hiding this comment

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

I find void useless, so I didn't add any.
If you want to make it your battle, please do :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Method without void can mean two types of things - either it's return type void, or it's just not filled in or the actual return type is in DocBlock. That's a lot of "or"s :)

For me, it would make sense to add it if it were to be enforced by phpcs (method either has return type hint, or @return docblock). Is there a plan to enforce return typehints?

Copy link
Member Author

Choose a reason for hiding this comment

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

no plan I know about

Copy link
Contributor

Choose a reason for hiding this comment

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

@@ -36,7 +36,7 @@
* @throws UnexpectedTypeException if a timezone is not a string
* @throws InvalidArgumentException if a timezone is not valid
*/
public function __construct($inputTimezone = null, $outputTimezone = null)
public function __construct(string $inputTimezone = null, string $outputTimezone = null)
{
if (null !== $inputTimezone && !is_string($inputTimezone)) {
throw new UnexpectedTypeException($inputTimezone, 'string');
Copy link
Contributor

@Tobion Tobion Nov 8, 2017

Choose a reason for hiding this comment

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

this check is useless now as it cannot happen anymore

Copy link
Member Author

Choose a reason for hiding this comment

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

see #24876

@@ -307,15 +307,15 @@ public function testReverseTransformWrapsIntlErrors()
}

/**
* @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
* @expectedException \TypeError
*/
public function testValidateDateFormatOption()
Copy link
Contributor

Choose a reason for hiding this comment

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

those tests are useless now as they only test php internal behavior

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm sure there are more left over, PR is so big...
Additional PR welcomed :)

fabpot added a commit that referenced this pull request Nov 9, 2017
This PR was merged into the 4.0-dev branch.

Discussion
----------

Remove some unneeded checks/tests

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

As spotted by @Tobion in #24722 (review)

Commits
-------

05681ec Remove some unneeded checks/tests
@TomasVotruba
Copy link
Contributor

Hey @nicolas-grekas , I'm curious, do you use (at least help of) automated tools for this, like TypeHintDeclarationSniff?

Checks for useless doc comments. If the native method declaration contains everything and the phpDoc does not add anything useful, it's reported as useless and can optionally be automatically removed with phpcbf.

I plan to add fixer who would take care of this deprecated-packages/symplify#416, because it's too time demanding and can be mostly automated, so I look for inspiration.

@nicolas-grekas
Copy link
Member Author

None, had to stay concentrated a few hours on it, would love a tool to help keep the docblocks low!

@TomasVotruba
Copy link
Contributor

TomasVotruba commented Nov 10, 2017

Holly cow 😲 ! I admire your persistence

I did something similar few days ago on php-ml: https://github.com/php-ai/php-ml/pull/145 with following setup:

composer require symplify/easy-coding-standard --dev
# clean-docs.neon
checkers:
    SlevomatCodingStandard\Sniffs\TypeHints\TypeHintDeclarationSniff:
        enableEachParameterAndReturnInspection: true
vendor/bin/ecs check src --config clean-docs.neon
# and to fix
vendor/bin/ecs check src --config clean-docs.neon --fix

TypeHintDeclarationSniff misses many cases and removes some annotations so it required ~40 minutes extra, but I'm working on fixer that should decrease this to 2 minutes extra (just scrolling the diff ;)).

Feel free to try this and let me know how it works for you.

@TomasVotruba
Copy link
Contributor

@nicolas-grekas Finished in dev version: https://github.com/Symplify/CodingStandard#block-comment-should-only-contain-useful-information-about-types

Used in these PR:

@fabpot fabpot mentioned this pull request Nov 12, 2017
nicolas-grekas added a commit that referenced this pull request Oct 14, 2018
…udaltsov)

This PR was merged into the 4.1 branch.

Discussion
----------

[Process] Allow to pass non-string arguments to Process

| Q             | A
| ------------- | ---
| Branch?       | 4.1
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #28609
| License       | MIT
| Doc PR        |

Sometimes you might pass integers, floats, nulls as command arguments to the Process constructor.
Before 4.0 and #24722 that worked fine. Now it throws because of an invalid type.

I think we can drop the type hint here safely and stringify the value implicitly in the method. That will be a little more convenient.

Commits
-------

acf8b83 Remove Process::escapeArgument argument type hint
fabpot added a commit that referenced this pull request Jun 17, 2019
This PR was merged into the 4.4 branch.

Discussion
----------

Fine tune constructor types

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets |
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

Fine tunes some constructor types that have been added in #24722

Form names as integer was only a workaround as forms names are used as array keys which get transformed to int. So it was added as a workaround in #6355 (comment)
With typehints added in #24722 those were mostly auto-cast anyway, e.g. in FormBuilder. There are only a few integer form names remaining documented, in the main entry points of the Form component (`\Symfony\Component\Form\FormInterface::add`). Internally it's always a string now. So I could remove some int docs which also fixes #30032 what @xabbuh tried to do.

Some of these changes we're just not done before because of broken tests. It's mainly a missing explicit mock for `TranslationInterface::trans` which returned null. If we had return types hints in interfaces, this wouldn't happen.

Commits
-------

507794a Fine tune constructor types
fabpot added a commit that referenced this pull request Jul 5, 2019
…e Segatori, tigitz)

This PR was squashed before being merged into the 5.0-dev branch (closes #32273).

Discussion
----------

[Process] [5.0] Replace docblocks by type-hints

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | continuation of #24722 and checks for  #32179
| License       | MIT
| Doc PR        | N/A

This PR adds replace docblocks by type hints in the Process component. Some docblocks without valuable information got also removed.

Commits
-------

5c964c5 [Process] [5.0] Replace docblocks by type-hints
nicolas-grekas added a commit that referenced this pull request Aug 8, 2019
…ippe Segatori)

This PR was merged into the 5.0-dev branch.

Discussion
----------

[HttpKernel] [5.0] Replace docblocks by type-hints

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | continuation of #24722 and checks for  #32179
| License       | MIT
| Doc PR        | N/A

This PR replaces docblocks by type hints in the HttpKernel component considering #32179. Some docblocks without valuable information got also removed.

<!--
Replace this notice by a short README for your feature/bugfix. This will help people
understand your PR and can be used as a start for the documentation.

Additionally (see https://symfony.com/roadmap):
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too).
 - Features and deprecations must be submitted against branch 4.4.
 - Legacy code removals go to the master branch.
-->

Commits
-------

9e570a2 [Http-Kernel][5.0] Add type-hints
nicolas-grekas added a commit that referenced this pull request Aug 8, 2019
…, Tobion)

This PR was squashed before being merged into the 5.0-dev branch (closes #32525).

Discussion
----------

[Intl][5.0] Add parameters type-hints

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | continuation of #24722 and checks for  #32179
| License       | MIT
| Doc PR        | N/A

This PR replaces docblocks by type hints in the Intl component considering #32179. Some docblocks without valuable information got also removed.

Commits
-------

ce79f4b [Intl][5.0] Add parameters type-hints
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants