-
Notifications
You must be signed in to change notification settings - Fork 2k
Improvements for getSegment() method from URI class #2952
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
Improvements for getSegment() method from URI class #2952
Conversation
|
Don't validate anything: How can this even be true? You don't suppress the message, so it should throw an error. You don't need the silent part. If it existed, it would be populated or give you the default value. public function getSegment(int $number, string $default = ''): string
{
// The segment should treat the array as 1-based for the user
// but we still have to deal with a zero-based array.
$number -= 1;
if ($default !== '' && $number > count($this->segments))
{
throw HTTPException::forURISegmentOutOfRange($number);
}
return $this->segments[$number] ?? $default;
}Personally I would love to see this instead, but that means it gives you null in some cases. (Haven't tested it, if there are cases where it dosen't exist, and will give you null.) And that will breaks things I guess. :-( public function getSegment(int $number, string $default = null): ?string
{
// The segment should treat the array as 1-based for the user
// but we still have to deal with a zero-based array.
$number -= 1;
if (! is_null($default) && $number > count($this->segments))
{
throw HTTPException::forURISegmentOutOfRange($number);
}
return $this->segments[$number] ?? $default;
} |
Well... you have to look at the code and check how it is working now. The Exception is thrown only if you're trying to get a segment that is greater than About the part that proposes to skip the This implementation below wouldn't allow us to return an empty string for segments that don't exist - rather unacceptable.
And this one, as you pointed out, would return
So, that's why the |
|
Yeah, sorry about that. That's even more stupid in my opinion. Why would you allow fetching something that's not there. So I didn't think about +1 thing. setSegment() works the same now that I look at it. You can set it +2 more than there are segments. It still gets appended as +1. Did some actual testing with this one now, instead of just writing from memory. I can't get any segment with '', unless I have manually used setSegment(). Are there any special cases I'm missing? As I can't trigger this one. I get the same result with the one that we had before. Too bad we have made a real release, because "fixing" the problem will require all programmers to use three parameters. As we can't do BC changes. Personally I get the same result with this one. Less code yay! public function getSegment(int $number, ?string $default = '', bool $silent = false): ?string
{
// The segment should treat the array as 1-based for the user
// but we still have to deal with a zero-based array.
$number -= 1;
if (! $silent && $number > count($this->segments))
{
throw HTTPException::forURISegmentOutOfRange($number);
}
return $this->segments[$number] ?? $default;
}Seeing how this works, I'm just going to replace it, if I will ever need to use it. I have already replaced some, as they differ to much from they "should" work, based on how other frameworks have solved the same thing. public function getSegment(int $number, string $default = null): ?string
{
return $this->segments[$number - 1] ?? $default;
} |
|
The thing with
Yeah... you are right. I have no idea why I changed it - I will send an update. Thanks.
I guess every framework has its own "things". And "this thing" isn't something really terrible - as long as this change will be merged ;P We all have some habits and expectations about how something "should" works. And the truth is that before the v4 release there were not so many real-world applications built, so there are probably still a few things to polish up. |
|
If you only want valid positions with How's your take on adding a Of course it will be differences, but CI4 are the only major player that throws an error message for this. Sure you can set a standard. // You are more likely to use an if statement than use it an try block.
if(($id = $this->request->uri->getSegment(3)) === null)
{
throw new Exception('No id found...');
}
// Instead of
try
{
$id = $this->request->uri->getSegment(3);
}
catch(Exception $e)
{
throw new Exception('No id found...');
}
// Or this to be able to use it in an if statement later.
try
{
$id = $this->request->uri->getSegment(3);
}
catch(Exception $e)
{
$id = null
}
if($id === null)
{
// Load error to user
}
else
{
// Load real content
} |
I haven't looked at
In some applications, I used it heavily - ie. when we had many categories with different options. We didn't always have a full URL, so support for this and default values were a real saver there.
Personally, I would prefer one-liner with three parameters than catching the exception every single time. The other advantage is that you can set a default value really easily. |
|
I was thinking of a |
|
Should have also specified that I think this is a good change and a very nice PR! Thanks for the work, and for the review from @jreklund |
|
I think after the setSilent() method we could implement a default value as well (but without $silent of course), as it's a nice feature. :-) |
Description
This PR introduces some improvements for
URI::getSegment()method, adding two optional parameters:$default- default value to use if given segment is empty$silent- if set totrueit prevents throwing an exceptionThis is related to the issue #2949
I think that this might be very handy in some situations. Handling the scenario where we want to check if given parameter is out there is not very friendly for developers right now. These changes will save some of us a lot of code.
Checklist: