Skip to content

Commit 0023cb1

Browse files
authored
Merge pull request #7853 from nextcloud/strict_urlgenerator
Make the URLGenerator strict
2 parents d44de92 + a3b33be commit 0023cb1

File tree

3 files changed

+27
-34
lines changed

3 files changed

+27
-34
lines changed

lib/private/URLGenerator.php

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* @copyright Copyright (c) 2016, ownCloud, Inc.
45
*
@@ -34,12 +35,10 @@
3435

3536
namespace OC;
3637

37-
3838
use OCP\ICacheFactory;
3939
use OCP\IConfig;
4040
use OCP\IRequest;
4141
use OCP\IURLGenerator;
42-
use OCP\Route\IRoute;
4342
use RuntimeException;
4443

4544
/**
@@ -74,10 +73,9 @@ public function __construct(IConfig $config,
7473
*
7574
* Returns a url to the given route.
7675
*/
77-
public function linkToRoute($route, $parameters = array()) {
76+
public function linkToRoute(string $route, array $parameters = array()): string {
7877
// TODO: mock router
79-
$urlLinkTo = \OC::$server->getRouter()->generate($route, $parameters);
80-
return $urlLinkTo;
78+
return \OC::$server->getRouter()->generate($route, $parameters);
8179
}
8280

8381
/**
@@ -88,7 +86,7 @@ public function linkToRoute($route, $parameters = array()) {
8886
*
8987
* Returns an absolute url to the given route.
9088
*/
91-
public function linkToRouteAbsolute($routeName, $arguments = array()) {
89+
public function linkToRouteAbsolute(string $routeName, array $arguments = array()): string {
9290
return $this->getAbsoluteURL($this->linkToRoute($routeName, $arguments));
9391
}
9492

@@ -102,20 +100,20 @@ public function linkToRouteAbsolute($routeName, $arguments = array()) {
102100
*
103101
* Returns a url to the given app and file.
104102
*/
105-
public function linkTo( $app, $file, $args = array() ) {
103+
public function linkTo(string $app, string $file, array $args = array()): string {
106104
$frontControllerActive = ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true');
107105

108-
if( $app != '' ) {
106+
if( $app !== '' ) {
109107
$app_path = \OC_App::getAppPath($app);
110108
// Check if the app is in the app folder
111109
if ($app_path && file_exists($app_path . '/' . $file)) {
112-
if (substr($file, -3) == 'php') {
110+
if (substr($file, -3) === 'php') {
113111

114112
$urlLinkTo = \OC::$WEBROOT . '/index.php/apps/' . $app;
115113
if ($frontControllerActive) {
116114
$urlLinkTo = \OC::$WEBROOT . '/apps/' . $app;
117115
}
118-
$urlLinkTo .= ($file != 'index.php') ? '/' . $file : '';
116+
$urlLinkTo .= ($file !== 'index.php') ? '/' . $file : '';
119117
} else {
120118
$urlLinkTo = \OC_App::getAppWebPath($app) . '/' . $file;
121119
}
@@ -150,7 +148,7 @@ public function linkTo( $app, $file, $args = array() ) {
150148
*
151149
* Returns the path to the image.
152150
*/
153-
public function imagePath($app, $image) {
151+
public function imagePath(string $app, string $image): string {
154152
$cache = $this->cacheFactory->createDistributed('imagePath-'.md5($this->getBaseUrl()).'-');
155153
$cacheKey = $app.'-'.$image;
156154
if($key = $cache->get($cacheKey)) {
@@ -210,9 +208,9 @@ public function imagePath($app, $image) {
210208
if($path !== '') {
211209
$cache->set($cacheKey, $path);
212210
return $path;
213-
} else {
214-
throw new RuntimeException('image not found: image:' . $image . ' webroot:' . \OC::$WEBROOT . ' serverroot:' . \OC::$SERVERROOT);
215211
}
212+
213+
throw new RuntimeException('image not found: image:' . $image . ' webroot:' . \OC::$WEBROOT . ' serverroot:' . \OC::$SERVERROOT);
216214
}
217215

218216

@@ -221,15 +219,15 @@ public function imagePath($app, $image) {
221219
* @param string $url the url in the ownCloud host
222220
* @return string the absolute version of the url
223221
*/
224-
public function getAbsoluteURL($url) {
222+
public function getAbsoluteURL(string $url): string {
225223
$separator = $url[0] === '/' ? '' : '/';
226224

227-
if (\OC::$CLI && !defined('PHPUNIT_RUN')) {
225+
if (\OC::$CLI && !\defined('PHPUNIT_RUN')) {
228226
return rtrim($this->config->getSystemValue('overwrite.cli.url'), '/') . '/' . ltrim($url, '/');
229227
}
230228
// The ownCloud web root can already be prepended.
231-
if(substr($url, 0, strlen(\OC::$WEBROOT)) === \OC::$WEBROOT) {
232-
$url = substr($url, strlen(\OC::$WEBROOT));
229+
if(substr($url, 0, \strlen(\OC::$WEBROOT)) === \OC::$WEBROOT) {
230+
$url = substr($url, \strlen(\OC::$WEBROOT));
233231
}
234232

235233
return $this->getBaseUrl() . $separator . $url;
@@ -239,15 +237,15 @@ public function getAbsoluteURL($url) {
239237
* @param string $key
240238
* @return string url to the online documentation
241239
*/
242-
public function linkToDocs($key) {
240+
public function linkToDocs(string $key): string {
243241
$theme = \OC::$server->getThemingDefaults();
244242
return $theme->buildDocLinkToKey($key);
245243
}
246244

247245
/**
248246
* @return string base url of the current request
249247
*/
250-
public function getBaseUrl() {
248+
public function getBaseUrl(): string {
251249
return $this->request->getServerProtocol() . '://' . $this->request->getServerHost() . \OC::$WEBROOT;
252250
}
253251
}

lib/public/IURLGenerator.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* @copyright Copyright (c) 2016, ownCloud, Inc.
45
*
@@ -24,14 +25,6 @@
2425
*
2526
*/
2627

27-
/**
28-
* Public interface of ownCloud for apps to use.
29-
* URL generator interface
30-
*
31-
*/
32-
33-
// use OCP namespace for all classes that are considered public.
34-
// This means that they should be used by apps instead of the internal ownCloud classes
3528
namespace OCP;
3629

3730
/**
@@ -46,7 +39,7 @@ interface IURLGenerator {
4639
* @return string the url
4740
* @since 6.0.0
4841
*/
49-
public function linkToRoute($routeName, $arguments = array());
42+
public function linkToRoute(string $routeName, array $arguments = array()): string;
5043

5144
/**
5245
* Returns the absolute URL for a route
@@ -55,7 +48,7 @@ public function linkToRoute($routeName, $arguments = array());
5548
* @return string the absolute url
5649
* @since 8.0.0
5750
*/
58-
public function linkToRouteAbsolute($routeName, $arguments = array());
51+
public function linkToRouteAbsolute(string $routeName, array $arguments = array()): string;
5952

6053
/**
6154
* Returns an URL for an image or file
@@ -66,7 +59,7 @@ public function linkToRouteAbsolute($routeName, $arguments = array());
6659
* @return string the url
6760
* @since 6.0.0
6861
*/
69-
public function linkTo($appName, $file, $args = array());
62+
public function linkTo(string $appName, string $file, array $args = array()): string;
7063

7164
/**
7265
* Returns the link to an image, like linkTo but only with prepending img/
@@ -75,7 +68,7 @@ public function linkTo($appName, $file, $args = array());
7568
* @return string the url
7669
* @since 6.0.0
7770
*/
78-
public function imagePath($appName, $file);
71+
public function imagePath(string $appName, string $file): string;
7972

8073

8174
/**
@@ -84,18 +77,18 @@ public function imagePath($appName, $file);
8477
* @return string the absolute version of the url
8578
* @since 6.0.0
8679
*/
87-
public function getAbsoluteURL($url);
80+
public function getAbsoluteURL(string $url): string;
8881

8982
/**
9083
* @param string $key
9184
* @return string url to the online documentation
9285
* @since 8.0.0
9386
*/
94-
public function linkToDocs($key);
87+
public function linkToDocs(string $key): string;
9588

9689
/**
9790
* @return string base url of the current request
9891
* @since 13.0.0
9992
*/
100-
public function getBaseUrl();
93+
public function getBaseUrl(): string;
10194
}

tests/Settings/Mailer/NewUserMailHelperTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public function setUp() {
6060
parent::setUp();
6161

6262
$this->defaults = $this->createMock(Defaults::class);
63+
$this->defaults->method('getLogo')
64+
->willReturn('myLogo');
6365
$this->urlGenerator = $this->createMock(IURLGenerator::class);
6466
$this->l10n = $this->createMock(IL10N::class);
6567
$this->mailer = $this->createMock(IMailer::class);

0 commit comments

Comments
 (0)