-
-
Notifications
You must be signed in to change notification settings - Fork 344
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 Relative Path to Report Output #704
Comments
Hello Tom, It is a nice idea to add this as an option. I think the most easy option is to add a command line option that strip the base directory from the filename. Feel free to create a PR for this. |
I think it’d be best if we introduce a |
Yes, good idea, |
Would you store this next to |
Next to file sounds good for me. 👍 But maybe is fileName not a correct one, in your example I should suspect that fileName is Class.php. Maybe relativeFile or something like that. |
What about |
I'd opt for |
Perfect. |
/**
* Returns the base name where this rule violation was detected.
*
* @return string
*/
public function getBaseName()
{
var_dump($this->getFileName());
var_dump(basename($this->getFileName()));
return $this->getFileName();
} Currently outputs:
It strips out too much information, at the moment. I'll try and find the correct PHP function to use. |
Oups, my mistake, indeed baseName was not what we're looking for. |
Psalm does it as follows: https://github.com/vimeo/psalm/blob/a154191922d14dcdde8ef348520a88d735d82304/src/Psalm/Config.php#L1162 but I'm sure there must be an easy/easier way? |
It's about relative path (implicitly relative to current directory getcwd which is where you run the And the Psalm solution will still return an absolute path for paths outside the baseDir (but I guess this case can't happen). Relative path from 2 paths can be calculated this way: $cwd = explode(DIRECTORY_SEPARATOR, getcwd());
$path = explode(DIRECTORY_SEPARATOR, realpath('some-directory/some-file.php'));
foreach ($cwd as $index => $directory) {
if ($directory !== $path[$index]) {
break;
}
unset($path[$index]);
unset($cwd[$index]);
}
echo str_repeat('../', count($cwd)).implode('/', $path); This will be correct even if you run |
In our case |
I’ll give this a whirl in a bit. Cheers! |
This did the trick in my initial test! /**
* Returns the base name where this rule violation was detected.
*
* @return string
*/
public function getBaseName()
{
$cwd = explode(DIRECTORY_SEPARATOR, getcwd());
$path = explode(DIRECTORY_SEPARATOR, realpath($this->getFileName()));
foreach ($cwd as $index => $directory) {
if ($directory !== $path[$index]) {
break;
}
unset($path[$index]);
unset($cwd[$index]);
}
var_dump(str_repeat('../', count($cwd)).implode('/', $path));
return $this->getFileName();
}
|
The new output is as follows: {
"version": "@project.version@",
"package": "phpmd",
"timestamp": "2019-12-06T10:52:04+00:00",
"files": [
{
"file": "\/var\/www\/html\/app\/Console\/Kernel.php",
"baseName": "app\/Console\/Kernel.php",
"violations": [
{
"beginLine": 25,
"endLine": 25,
"package": null,
"function": null,
"class": null,
"method": null,
"description": "Avoid unused parameters such as \u0027$schedule\u0027.",
"rule": "UnusedFormalParameter",
"ruleSet": "Unused Code Rules",
"externalInfoUrl": "https:\/\/phpmd.org\/rules\/unusedcode.html#unusedformalparameter",
"priority": 3
}
]
}
]
} |
Thanks @ttomdewit! Would you be OK to create a pull-request for that and ideally add a unit test that cover this new feature? So you'll get your name on this contribution. |
I would very much like that! I'll try and get that sorted today! |
I started working on a PR: #706 |
Thanks! In this PR we can focus on JSON renderer but I think some other renderers (like the XML one) may also benefit of this adding. |
I agree. Maybe we can move the new method up so all renderers benefit from it? See #706 (comment) for details |
Yes, what the actual goal here is the "relative path", nothing else. Thus, we should name it like that. I'm going to edit the title of this issue, too, to reflect the new goal.
I guess this is out of scope of this issue/PR. And we cannot change existing report field data becaus of backwards compatibility. |
Support question
Thanks for all you do for the PHP community!
Would it be in the scope of this project to allow printing short filenames in the output?
For example, the current output is as follows:
What I'd personally need is as follows:
The changes I'm requesting can be found at files -> file and at https://github.com/phpmd/phpmd/blob/master/src/main/php/PHPMD/Renderer/JSONRenderer.php#L73.
Thank you for your time.
Tom
The text was updated successfully, but these errors were encountered: