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

Add optional filename and line number information to patterns #34

Merged
merged 5 commits into from
Oct 2, 2024
Merged

Add optional filename and line number information to patterns #34

merged 5 commits into from
Oct 2, 2024

Conversation

kellegous
Copy link
Contributor

We would like to be able to indicate the line number for the relevant pattern in error/warning messages in a project. This pull request adds a nullable SourceInfo property to Pattern that may be optionally via the constructor. The Parser has been updated to always populate that property when parsing via parseFile.

When using parseString, the filename defaults to an empty string ('') but can be overwritten via the $filename parameter. Having the parameter optional maintains backwards compatibility but also makes source info available on both parse methods in the parser.

@timoschinkel
Copy link
Owner

Thank you for your contribution. I will have a look at the proposed changes.

@timoschinkel
Copy link
Owner

I like the idea, but two things are preventing me from merging as is; this adds new functionality, so following semantic versioning this should create version 2.3.0.

I'm wondering about what the default value for $filename should be for Parser::parseString(). Right now you have made it a nullable string defaulting to an empty string. Looking at the updated tests this is intentional. But the default value for the (private) Parser::parseIterable() is null. I think having the line number is valuable information, independent whether you have specified a filename.

Would it make sense to change the signature to Parser::parseString(string $lines) (so no filename), and make SourceInfo::$filename nullable. That way it is possible to always have a line number in the rule.

If you can come up with a good use-case for specifying a filename when parsing from a string, then I am also okay with using the signature Parser::parseString(string $lines, ?string $filename = null).

@kellegous
Copy link
Contributor Author

I like the idea, but two things are preventing me from merging as is; this adds new functionality, so following semantic versioning this should create version 2.3.0.

Absolutely, you're right, this adds new backwards compatible public API. Will fix.

Would it make sense to change the signature to Parser::parseString(string $lines) (so no filename), and make SourceInfo::$filename nullable. That way it is possible to always have a line number in the rule.

I was torn on how to handle Parser::parseString(string $lines) and flip-flopped on a null filename or an empty filename. I'll change SourceInfo::getFilename to return a ?string and we should be good there.

As to the use case for Parse::parseString($lines, $filename), here's what I had in mind, which is a somewhat common pattern for parsers that either take a path or raw contents (in this case a string).

$data_from_owners = file_get_contents('OWNERS');
// just an example, the idea is that you often need to do something with the contents of the file you will
// be parsing, but you still want to parse the contents as if they had been read directly from a file.
$digest = hash('sha256', $data_from_owners); 
$patterns = Parser::parseString($data_from_owners, 'OWNERS');

The idea is that there are cases where you are ultimately reading from a file but you need to have access to the data that resides in the file prior to parsing it. All of that said, though, this is not a use case I currently have. It is, indeed, speculative. As the maintainer, I'll defer to you if you think the speculative use is worth it.

Thank you for the review. I'll update this PR soon.

1. Increment minor semvar version.
2. Make filename property nullable for SourceInfo.
3. Parser::parseString($filename) includes SourceInfo on patterns but w/ null filename.
@kellegous
Copy link
Contributor Author

Updated. Just let me know if you think the use case for passing $filename to Parser::parseString is worthwhile. If not, I'll remove it.

Copy link
Owner

@timoschinkel timoschinkel left a comment

Choose a reason for hiding this comment

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

Let's make the SourceInfo not nullable. If that's done I'll merge this one.

src/Parser.php Outdated Show resolved Hide resolved
@timoschinkel timoschinkel merged commit 3ce6451 into timoschinkel:main Oct 2, 2024
4 checks passed
@timoschinkel
Copy link
Owner

Thanks for the contribution. I've merged and tagged version 2.3.0.

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