bug #21805 Provide less state in getRequestFormat (dawehner)

This PR was submitted for the master branch but it was merged into the 2.7 branch instead (closes #21805).

Discussion
----------

Provide less state in getRequestFormat

Let's assume you multiple lines of code calling to ```Request::getRequestFormat```providing different default values.

```
$request->getRequestFormat();
$request->getRequestFormat('json')
```

As of HEAD this causes the second call to return 'html', unless its set explicit via ```setRequestFormat()```.

IMHO this is state which can be avoided.

Note: This also helps Drupal.

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | no
| Fixed tickets |
| License       | MIT
| Doc PR        |

Commits
-------

1d43007 Provide less state in getRequestFormat
This commit is contained in:
Nicolas Grekas 2017-03-04 11:53:39 +01:00
commit d45008d2d1
2 changed files with 7 additions and 2 deletions

View File

@ -1382,10 +1382,10 @@ class Request
public function getRequestFormat($default = 'html')
{
if (null === $this->format) {
$this->format = $this->get('_format', $default);
$this->format = $this->get('_format');
}
return $this->format;
return null === $this->format ? $default : $this->format;
}
/**

View File

@ -1401,6 +1401,11 @@ class RequestTest extends TestCase
$request = new Request();
$this->assertEquals('html', $request->getRequestFormat());
// Ensure that setting different default values over time is possible,
// aka. setRequestFormat determines the state.
$this->assertEquals('json', $request->getRequestFormat('json'));
$this->assertEquals('html', $request->getRequestFormat('html'));
$request = new Request();
$this->assertNull($request->getRequestFormat(null));