Skip to content

phpDoc annotation spacing #88

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 3 commits into from
Sep 22, 2018
Merged

phpDoc annotation spacing #88

merged 3 commits into from
Sep 22, 2018

Conversation

Majkl578
Copy link
Contributor

Sniff to require a newline between between description and tags, a newline between annotation groups and no newline before/after phpDoc content.

Example:

    /**
     * Description
     * More Description
     *
     * @internal
     * @deprecated
     *
     * @link https://example.com
     * @see  other
     * @uses other
     *
     * @ORM\Id
     * @ORM\Column
     * @ODM\Id
     * @ODM\Column
     *
     * @param int[] $foo
     * @param int[] $bar
     *
     * @return int[]
     *
     * @throws FooException
     * @throws BarException
     */
    public function d(iterable $foo, iterable $bar) : iterable
    {
    }

This is based on spacing/format used in ORM, does this properly fit other projects (i.e. ODM) too?

Changes in ORM with this enabled are mostly (superfluous newlines) cosmetical: Majkl578/doctrine-orm@68920f1

@Majkl578 Majkl578 requested a review from a team as a code owner September 20, 2018 14:24
if dpkg --compare-versions "$(composer show -n | grep squizlabs/php_codesniffer | tr -s ' ' | cut -d ' ' -f 2)" '>=' "3.3.2"; then
xmllint --noout --schema vendor/squizlabs/php_codesniffer/phpcs.xsd lib/Doctrine/ruleset.xml
else
echo "Validation skipped, PHPCS 3.3.2+ required"
Copy link
Contributor Author

@Majkl578 Majkl578 Sep 20, 2018

Choose a reason for hiding this comment

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

Since this PR requires new property syntax for arrays, I put this conditional check in place. The ruleset is valid according to XML schema from 3.3.2-dev.

Copy link
Member

@morozov morozov Sep 20, 2018

Choose a reason for hiding this comment

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

Right now, we allow PHP_CodeSniffer ^3.1.1. Given that the schema is invalid for versions <= 3.3.1, will PHP_CodeSniffer 3.1.1 be able to process those rules?

Judging by the milestone of squizlabs/PHP_CodeSniffer#2154, the new schema will be available in 3.4.0, not in 3.3.2.

Why don't we just wait for a new release and bump the requirement? Otherwise, we may run into a PHP 6 situation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes it will, PHPCS itself doesn't validate the schema.
The referenced PR is not related to this part, we don't use the proposed extend here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addresses this problem: #84 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

So what is the 3.3.2 change which the XML validation depends on?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

squizlabs/PHP_CodeSniffer#2151

They forgot to update the schema to include the new <element> syntax for arrays (available since 3.3.0).

Copy link
Member

Choose a reason for hiding this comment

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

Gotcha. It'd be nice if they had validation internally as in PHPUnit (sebastianbergmann/phpunit#3048). Otherwise, the schema validity will have to be enforced by 3rd party's CI like ours.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Possibly something for PHPCS 4.0. :)

@malarzm
Copy link
Member

malarzm commented Sep 20, 2018

From what I see in ODM we used to have no newline between @param and @return but I had to check 1.2 branch to find that, with 2.0 most param/return phpdocs got removed anyway :D

@deeky666
Copy link
Member

I like this idea. Just for clarification: What exactly is a "annotation group"? Considering your example I would expect a newline between the @ORM and @ODM tags, no?

@Majkl578
Copy link
Contributor Author

@deeky666 My assumption was that you either:

  • don't mix those two on a single object as they're mutually exclusive (this is imho normal state in regular apps - an entity is either an ORM object or an ODM object),
  • mix those in a generic package (i.e. some shared Entity) and then you may want both @O[RD]M\Id etc. together

But I may be wrong, I never worked with a hybrid application.

@deeky666
Copy link
Member

@Majkl578 sure in this example it might not make much sense but if you have other annotation groups like from JMS/Serializer or whatever, I would not expect to mix those up with @ORM / @ODM

@Majkl578
Copy link
Contributor Author

@deeky666 For annotations (or annotation namespaces) unspecied here, all unknown are put into a special group that is added at the end. Check the description here:

Annotations not in any group are placed to automatically created last group.

So for i.e. @JMS\Type, it'd end up just before */. These would be up to you to configure this per project, we can't provide defaults for all possible annotations/namespaces.

@Ocramius Ocramius self-assigned this Sep 22, 2018
@Ocramius Ocramius added this to the 5.0.0 milestone Sep 22, 2018
Copy link
Member

@Ocramius Ocramius left a comment

Choose a reason for hiding this comment

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

🚢

@Ocramius Ocramius merged commit da9015f into master Sep 22, 2018
@Ocramius Ocramius deleted the sniff/DocCommentSpacing branch September 22, 2018 12:45
@lcobucci lcobucci mentioned this pull request Jan 31, 2019
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.

8 participants