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

String literal matching specific regex annotation #154

Open
janbarasek opened this issue Jan 17, 2022 · 1 comment
Open

String literal matching specific regex annotation #154

janbarasek opened this issue Jan 17, 2022 · 1 comment

Comments

@janbarasek
Copy link

Hello,

Often I want to prepare a package for my colleagues that defines very strict rules about the expectation of what input strings a method argument or property can handle.

If I implement the format validation as an assert, we will only learn about the error at runtime and it may never occur. If there is a native way to define the expected string format, it will be possible to automatically check huge code structures at compiletime.

For example (possible value can be v1.2.3):

function getFirstVersionPart(string $version): int
{
   preg_match('/^v(\d+)/', $version, $parser);

   return (int) $parser[1];
}

This annotation would help PhpStan recognize how to evaluate a regular expression and that all indexes will exist (and I don't need to handle the occurrence of the 1 index).

Thanks.

@ondrejmirtes
Copy link
Member

Hi, unless you find a similar feature in a different language like TypeScript or Hacklang, I'm not really interested 😊

Personally I'd pass around objects instead of strings.

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

No branches or pull requests

2 participants