Skip to content
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

a bit of a clean up for regexes #146

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.IncorrectReturnTypeHint"/>
</rule>

<rule ref="Generic.Files.LineLength">
<exclude-pattern>src/Roave/SecurityAdvisories/Matchers.php</exclude-pattern>
</rule>

<file>build-conflicts.php</file>
<file>public</file>
<file>src</file>
Expand Down
2 changes: 1 addition & 1 deletion src/Roave/SecurityAdvisories/Flag.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
final class Flag
{
/**
* within extent of same version patch flag is of the highest priority
* within extent of the same version "patch" flag is of the highest priority
Copy link
Member

Choose a reason for hiding this comment

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

This reads very clunky 😬

* e.g. 1.1-alpha < 1.1-beta < 1.1-rc < 1.1-stable < 1.1 < 1.1-p
*/
private const PRIORITY = [
Expand Down
57 changes: 19 additions & 38 deletions src/Roave/SecurityAdvisories/Matchers.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,25 @@

namespace Roave\SecurityAdvisories;

/**
* @see https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
*
* @fixme: throw this garbage away and use existing regexp from semver.org
*/
final class Matchers
{
// pattern that matches full version only, without boundary sign
public const TAGGED_VERSION_MATCHER = '\s*(?<version>(?:\d+\.)*\d+)' .
'(?:-' . // dash is required for correct version
'(?<flag>stable|beta|b|rc|alpha|a|patch|p)' .
'[._-]?' .
'(?<stability_numbers>(?:\d+\.)*\d+)?' .
')?\s*';

private const UNTAGGED_VERSION_MATCHER = '((?:\d+\.)*\d+)' .
'(?:-' .
'(stable|beta|b|rc|alpha|a|patch|p)' .
'[._-]?' .
'((?:\d+\.)*\d+)?' .
')?';

// pattern that ensures we have a correct boundary in the right place
public const BOUNDARY_MATCHER = '/^\s*(?<boundary><|<=|=|>=|>)\s*' .
self::TAGGED_VERSION_MATCHER .
'\s*$/';

public const CLOSED_RANGE_MATCHER = '/^>(=?)\s*' .
self::UNTAGGED_VERSION_MATCHER .
'\s*,\s*<(=?)\s*' .
self::UNTAGGED_VERSION_MATCHER .
'$/';

public const LEFT_OPEN_RANGE_MATCHER = '/^<(=?)\s*' .
self::UNTAGGED_VERSION_MATCHER .
'$/';

public const RIGHT_OPEN_RANGE_MATCHER = '/^>(=?)\s*' .
self::UNTAGGED_VERSION_MATCHER .
'$/';
/*
* Pattern that matches full version only, without boundary sign.
* Was "inspired" by semver regexp -- https://github.com/composer/semver/blob/master/src/VersionParser.php
* Regular expression was tailored to the needs of the package and catches:
* - main version, e.g. 2.1.0
* - stability flag, e.g. alpha, beta and etc.
* - stability numbers
*/
public const TAGGED_VERSION_MATCHER = '\s*(?<version>(?:\d+\.)*\d+)(?:-(?<flag>stable|beta|b|rc|alpha|a|patch|p)[._-]?(?<stability_numbers>(?:\d+\.)*\d+)?)?\s*';
Copy link
Member

Choose a reason for hiding this comment

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

Is there any functional change in this patch?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is no functional change in this patch, just wanted it to be a bit more clean.

Copy link
Member

Choose a reason for hiding this comment

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

I must say that I find the previous version cleaner/easier to follow :|

Copy link
Contributor Author

Choose a reason for hiding this comment

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

okay, then lets close it. No worries.
Sorry for the noise ☺️

Copy link
Member

Choose a reason for hiding this comment

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

Alright, thanks for the effort anyway!


private const UNTAGGED_VERSION_MATCHER = '((?:\d+\.)*\d+)(?:-(stable|beta|b|rc|alpha|a|patch|p)[._-]?((?:\d+\.)*\d+)?)?';

public const BOUNDARY_MATCHER = '/^\s*(?<boundary><|<=|=|>=|>)\s*' . self::TAGGED_VERSION_MATCHER . '\s*$/';

public const CLOSED_RANGE_MATCHER = '/^>(=?)\s*' . self::UNTAGGED_VERSION_MATCHER . '\s*,\s*<(=?)\s*' . self::UNTAGGED_VERSION_MATCHER . '$/';

public const LEFT_OPEN_RANGE_MATCHER = '/^<(=?)\s*' . self::UNTAGGED_VERSION_MATCHER . '$/';

public const RIGHT_OPEN_RANGE_MATCHER = '/^>(=?)\s*' . self::UNTAGGED_VERSION_MATCHER . '$/';
}