Skip to content

Commit

Permalink
Request->getHeader() should always return a string
Browse files Browse the repository at this point in the history
PHPDoc (of the public API) says that this method returns string but it also returns null, which is not allowed in some method calls. This fixes that behaviour and returns an empty string and fixes all code paths that explicitly checked for null to be still compliant.

Found while enabling the strict_typing for lib/private for the PHP7+ migration.

Signed-off-by: Morris Jobke <[email protected]>
  • Loading branch information
MorrisJobke committed Jan 17, 2018
1 parent 22b3280 commit fe4ab92
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 16 deletions.
4 changes: 2 additions & 2 deletions apps/workflowengine/lib/Check/FileSize.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ protected function getFileSizeFromHeader() {
}

$size = $this->request->getHeader('OC-Total-Length');
if ($size === null) {
if ($size === '') {
if (in_array($this->request->getMethod(), ['POST', 'PUT'])) {
$size = $this->request->getHeader('Content-Length');
}
}

if ($size === null) {
if ($size === '') {
$size = false;
}

Expand Down
2 changes: 1 addition & 1 deletion core/Controller/ClientFlowLoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function __construct($appName,
*/
private function getClientName() {
$userAgent = $this->request->getHeader('USER_AGENT');
return $userAgent !== null ? $userAgent : 'unknown';
return $userAgent !== '' ? $userAgent : 'unknown';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion core/Controller/CssController.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function getCss($fileName, $appName) {
private function getFile(ISimpleFolder $folder, $fileName, &$gzip) {
$encoding = $this->request->getHeader('Accept-Encoding');

if ($encoding !== null && strpos($encoding, 'gzip') !== false) {
if (strpos($encoding, 'gzip') !== false) {
try {
$gzip = true;
return $folder->getFile($fileName . '.gzip'); # Safari doesn't like .gz
Expand Down
2 changes: 1 addition & 1 deletion core/Controller/JsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function getJs($fileName, $appName) {
private function getFile(ISimpleFolder $folder, $fileName, &$gzip) {
$encoding = $this->request->getHeader('Accept-Encoding');

if ($encoding !== null && strpos($encoding, 'gzip') !== false) {
if (strpos($encoding, 'gzip') !== false) {
try {
$gzip = true;
return $folder->getFile($fileName . '.gzip'); # Safari doesn't like .gz
Expand Down
5 changes: 2 additions & 3 deletions lib/private/AppFramework/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public function getHeader($name) {

}

return null;
return '';
}

/**
Expand Down Expand Up @@ -404,8 +404,7 @@ public function getCookie($key) {
protected function getContent() {
// If the content can't be parsed into an array then return a stream resource.
if ($this->method === 'PUT'
&& $this->getHeader('Content-Length') !== 0
&& $this->getHeader('Content-Length') !== null
&& $this->getHeader('Content-Length') !== '0'
&& $this->getHeader('Content-Length') !== ''
&& strpos($this->getHeader('Content-Type'), 'application/x-www-form-urlencoded') === false
&& strpos($this->getHeader('Content-Type'), 'application/json') === false
Expand Down
2 changes: 1 addition & 1 deletion lib/private/L10N/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public function languageExists($app, $lang) {
*/
private function getLanguageFromRequest($app) {
$header = $this->request->getHeader('ACCEPT_LANGUAGE');
if ($header) {
if ($header !== '') {
$available = $this->findAvailableLanguages($app);

// E.g. make sure that 'de' is before 'de_DE'.
Expand Down
5 changes: 4 additions & 1 deletion lib/private/Log/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ public static function write($app, $message, $level) {
} else {
$user = '--';
}
$userAgent = $request->getHeader('User-Agent') ?: '--';
$userAgent = $request->getHeader('User-Agent');
if ($userAgent === '') {
$userAgent = '--';
}
$version = $config->getValue('version', '');
$entry = compact(
'reqId',
Expand Down
4 changes: 2 additions & 2 deletions lib/private/Memcache/APCu.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ static public function isAvailable() {
} elseif (!\OC::$server->getIniWrapper()->getBool('apc.enable_cli') && \OC::$CLI) {
return false;
} elseif (
version_compare(phpversion('apc'), '4.0.6') === -1 &&
version_compare(phpversion('apcu'), '5.1.0') === -1
version_compare(phpversion('apc') ?: '0.0.0', '4.0.6') === -1 &&
version_compare(phpversion('apcu') ?: '0.0.0', '5.1.0') === -1
) {
return false;
} else {
Expand Down
8 changes: 8 additions & 0 deletions tests/Core/Controller/ClientFlowLoginControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,10 @@ public function testGeneratePasswordWithPassword() {
->expects($this->once())
->method('getServerHost')
->willReturn('example.com');
$this->request
->expects($this->once())
->method('getHeader')
->willReturn('');

$expected = new Http\RedirectResponse('nc://login/server:http://example.com&user:MyLoginName&password:MyGeneratedToken');
$this->assertEquals($expected, $this->clientFlowLoginController->generateAppPassword('MyStateToken'));
Expand Down Expand Up @@ -583,6 +587,10 @@ public function testGeneratePasswordWithoutPassword() {
->expects($this->once())
->method('getServerHost')
->willReturn('example.com');
$this->request
->expects($this->once())
->method('getHeader')
->willReturn('');

$expected = new Http\RedirectResponse('nc://login/server:http://example.com&user:MyLoginName&password:MyGeneratedToken');
$this->assertEquals($expected, $this->clientFlowLoginController->generateAppPassword('MyStateToken'));
Expand Down
15 changes: 11 additions & 4 deletions tests/lib/L10N/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,16 @@ public function setUp() {

/**
* @param array $methods
* @param bool $mockRequestGetHeaderMethod
* @return Factory|\PHPUnit_Framework_MockObject_MockObject
*/
protected function getFactory(array $methods = []) {
protected function getFactory(array $methods = [], $mockRequestGetHeaderMethod = false) {
if ($mockRequestGetHeaderMethod) {
$this->request->expects($this->any())
->method('getHeader')
->willReturn('');
}

if (!empty($methods)) {
return $this->getMockBuilder(Factory::class)
->setConstructorArgs([
Expand Down Expand Up @@ -137,7 +144,7 @@ public function testFindLanguageWithNotExistingRequestLanguageAndExistingStoredU
}

public function testFindLanguageWithNotExistingRequestLanguageAndNotExistingStoredUserLanguage() {
$factory = $this->getFactory(['languageExists']);
$factory = $this->getFactory(['languageExists'], true);
$this->invokePrivate($factory, 'requestLanguage', ['de']);
$factory->expects($this->at(0))
->method('languageExists')
Expand Down Expand Up @@ -180,7 +187,7 @@ public function testFindLanguageWithNotExistingRequestLanguageAndNotExistingStor
}

public function testFindLanguageWithNotExistingRequestLanguageAndNotExistingStoredUserLanguageAndNotExistingDefault() {
$factory = $this->getFactory(['languageExists']);
$factory = $this->getFactory(['languageExists'], true);
$this->invokePrivate($factory, 'requestLanguage', ['de']);
$factory->expects($this->at(0))
->method('languageExists')
Expand Down Expand Up @@ -226,7 +233,7 @@ public function testFindLanguageWithNotExistingRequestLanguageAndNotExistingStor
}

public function testFindLanguageWithNotExistingRequestLanguageAndNotExistingStoredUserLanguageAndNotExistingDefaultAndNoAppInScope() {
$factory = $this->getFactory(['languageExists']);
$factory = $this->getFactory(['languageExists'], true);
$this->invokePrivate($factory, 'requestLanguage', ['de']);
$factory->expects($this->at(0))
->method('languageExists')
Expand Down

0 comments on commit fe4ab92

Please sign in to comment.