Skip to content

Commit

Permalink
silently deprecated methods trigger E_USER_DEPRECATED
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Mar 20, 2023
1 parent 3157cce commit e5b7619
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/Http/FileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function __construct(?array $value)
*/
public function getName(): string
{
trigger_error(__METHOD__ . '() is deprecated, use getUntrustedName()', E_USER_DEPRECATED);
return $this->name;
}

Expand Down
1 change: 1 addition & 0 deletions src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ public function getHeaders(): array
*/
public function getReferer(): ?UrlImmutable
{
trigger_error(__METHOD__ . '() is deprecated', E_USER_DEPRECATED);
return isset($this->headers['referer'])
? new UrlImmutable($this->headers['referer'])
: null;
Expand Down
3 changes: 2 additions & 1 deletion src/Http/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,10 @@ private function useNonstandardProxy(Url $url): ?string
}


/** @deprecated */
/** @deprecated use fromGlobals() */
public function createHttpRequest(): Request
{
trigger_error(__METHOD__ . '() is deprecated, use fromGlobals()', E_USER_DEPRECATED);
return $this->fromGlobals();
}
}
8 changes: 8 additions & 0 deletions src/Http/SessionSection.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public function remove(string|array|null $name = null): void
*/
public function __set(string $name, $value): void
{
trigger_error("Writing to \$session->$name is deprecated, use \$session->set('$name', \$value) instead", E_USER_DEPRECATED);
$this->session->autoStart(true);
$this->getData()[$name] = $value;
}
Expand All @@ -111,6 +112,7 @@ public function __set(string $name, $value): void
*/
public function &__get(string $name): mixed
{
trigger_error("Reading from \$session->$name is deprecated, use \$session->get('$name') instead", E_USER_DEPRECATED);
$this->session->autoStart(true);
$data = &$this->getData();
return $data[$name];
Expand All @@ -123,6 +125,7 @@ public function &__get(string $name): mixed
*/
public function __isset(string $name): bool
{
trigger_error("Using \$session->$name is deprecated, use \$session->get('$name') instead", E_USER_DEPRECATED);
$this->session->autoStart(false);
return isset($this->getData()[$name]);
}
Expand All @@ -134,6 +137,7 @@ public function __isset(string $name): bool
*/
public function __unset(string $name): void
{
trigger_error("Unset(\$session->$name) is deprecated, use \$session->remove('$name') instead", E_USER_DEPRECATED);
$this->remove($name);
}

Expand All @@ -144,6 +148,7 @@ public function __unset(string $name): void
*/
public function offsetSet($name, $value): void
{
trigger_error("Writing to \$session['$name'] is deprecated, use \$session->set('$name', \$value) instead", E_USER_DEPRECATED);
$this->__set($name, $value);
}

Expand All @@ -154,6 +159,7 @@ public function offsetSet($name, $value): void
*/
public function offsetGet($name): mixed
{
trigger_error("Reading from \$session['$name'] is deprecated, use \$session->get('$name') instead", E_USER_DEPRECATED);
return $this->get($name);
}

Expand All @@ -164,6 +170,7 @@ public function offsetGet($name): mixed
*/
public function offsetExists($name): bool
{
trigger_error("Using \$session['$name'] is deprecated, use \$session->get('$name') instead", E_USER_DEPRECATED);
return $this->__isset($name);
}

Expand All @@ -174,6 +181,7 @@ public function offsetExists($name): bool
*/
public function offsetUnset($name): void
{
trigger_error("Unset(\$session['$name']) is deprecated, use \$session->remove('$name') instead", E_USER_DEPRECATED);
$this->remove($name);
}

Expand Down
3 changes: 3 additions & 0 deletions src/Http/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ public function getHostUrl(): string
/** @deprecated use UrlScript::getBasePath() instead */
public function getBasePath(): string
{
trigger_error(__METHOD__ . '() is deprecated, use UrlScript object', E_USER_DEPRECATED);
$pos = strrpos($this->path, '/');
return $pos === false ? '' : substr($this->path, 0, $pos + 1);
}
Expand All @@ -291,13 +292,15 @@ public function getBasePath(): string
/** @deprecated use UrlScript::getBaseUrl() instead */
public function getBaseUrl(): string
{
trigger_error(__METHOD__ . '() is deprecated, use UrlScript object', E_USER_DEPRECATED);
return $this->getHostUrl() . $this->getBasePath();
}


/** @deprecated use UrlScript::getRelativeUrl() instead */
public function getRelativeUrl(): string
{
trigger_error(__METHOD__ . '() is deprecated, use UrlScript object', E_USER_DEPRECATED);
return substr($this->getAbsoluteUrl(), strlen($this->getBaseUrl()));
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Http/FileUpload.basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test('', function () {
'size' => 209,
]);

Assert::same('readme.txt', $upload->getName());
Assert::same('readme.txt', @$upload->getName()); // deprecated
Assert::same('readme.txt', $upload->getUntrustedName());
Assert::same('readme.txt', $upload->getSanitizedName());
Assert::same('path/to/readme.txt', $upload->getUntrustedFullPath());
Expand All @@ -47,7 +47,7 @@ test('', function () {
'size' => 209,
]);

Assert::same('../.image.png', $upload->getName());
Assert::same('../.image.png', $upload->getUntrustedName());
Assert::same('image.png', $upload->getSanitizedName());
Assert::same('../.image.png', $upload->getUntrustedFullPath());
Assert::same('image/png', $upload->getContentType());
Expand Down
2 changes: 1 addition & 1 deletion tests/Http/Request.invalidEncoding.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,5 @@ test('filtered data', function () {
Assert::null($request->getFile(INVALID));
Assert::null($request->getFile(CONTROL_CHARACTERS));
Assert::type(Nette\Http\FileUpload::class, $request->files['file1']);
Assert::same('', $request->files['file1']->name);
Assert::same('', $request->files['file1']->getUntrustedName());
});
10 changes: 5 additions & 5 deletions tests/Http/RequestFactory.proxy.forwarded.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ test('', function () {
$factory = new RequestFactory;
$factory->setProxy('127.0.0.1');
Assert::same('127.0.0.3', $factory->fromGlobals()->getRemoteAddress());
Assert::same('localhost', $factory->fromGlobals()->getRemoteHost());
Assert::same('localhost', @$factory->fromGlobals()->getRemoteHost()); // deprecated

$factory->setProxy('127.0.0.1/8');
Assert::same('23.75.45.200', $factory->fromGlobals()->getRemoteAddress());
Assert::same('a23-75-45-200.deploy.static.akamaitechnologies.com', $factory->fromGlobals()->getRemoteHost());
Assert::same('a23-75-45-200.deploy.static.akamaitechnologies.com', @$factory->fromGlobals()->getRemoteHost()); // deprecated

$url = $factory->fromGlobals()->getUrl();
Assert::same('http', $url->getScheme());
Expand All @@ -43,7 +43,7 @@ test('', function () {

$factory->setProxy('127.0.0.3');
Assert::same('23.75.45.200', $factory->fromGlobals()->getRemoteAddress());
Assert::same('a23-75-45-200.deploy.static.akamaitechnologies.com', $factory->fromGlobals()->getRemoteHost());
Assert::same('a23-75-45-200.deploy.static.akamaitechnologies.com', @$factory->fromGlobals()->getRemoteHost()); // deprecated

$url = $factory->fromGlobals()->getUrl();
Assert::same(8080, $url->getPort());
Expand All @@ -62,7 +62,7 @@ test('', function () {

$factory->setProxy('127.0.0.3');
Assert::same('2001:db8:cafe::17', $factory->fromGlobals()->getRemoteAddress());
Assert::same('2001:db8:cafe::17', $factory->fromGlobals()->getRemoteHost());
Assert::same('2001:db8:cafe::17', @$factory->fromGlobals()->getRemoteHost()); // deprecated

$url = $factory->fromGlobals()->getUrl();
Assert::same('2001:db8:cafe::18', $url->getHost());
Expand All @@ -79,7 +79,7 @@ test('', function () {

$factory->setProxy('127.0.0.3');
Assert::same('2001:db8:cafe::17', $factory->fromGlobals()->getRemoteAddress());
Assert::same('2001:db8:cafe::17', $factory->fromGlobals()->getRemoteHost());
Assert::same('2001:db8:cafe::17', @$factory->fromGlobals()->getRemoteHost()); // deprecated

$url = $factory->fromGlobals()->getUrl();
Assert::same(47832, $url->getPort());
Expand Down
8 changes: 4 additions & 4 deletions tests/Http/RequestFactory.proxy.x-forwarded.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ test('', function () {
$factory = new RequestFactory;
$factory->setProxy('127.0.0.1');
Assert::same('127.0.0.3', $factory->fromGlobals()->getRemoteAddress());
Assert::same('localhost', $factory->fromGlobals()->getRemoteHost());
Assert::same('localhost', @$factory->fromGlobals()->getRemoteHost()); // deprecated

$factory->setProxy('127.0.0.1/8');
Assert::same('23.75.45.200', $factory->fromGlobals()->getRemoteAddress());
Assert::same('a23-75-45-200.deploy.static.akamaitechnologies.com', $factory->fromGlobals()->getRemoteHost());
Assert::same('a23-75-45-200.deploy.static.akamaitechnologies.com', @$factory->fromGlobals()->getRemoteHost()); // deprecated

$url = $factory->fromGlobals()->getUrl();
Assert::same('otherhost', $url->getHost());
Expand All @@ -44,11 +44,11 @@ test('', function () {
$factory = new RequestFactory;
$factory->setProxy('10.0.0.0/24');
Assert::same('172.16.0.1', $factory->fromGlobals()->getRemoteAddress());
Assert::same('172.16.0.1', $factory->fromGlobals()->getRemoteHost());
Assert::same('172.16.0.1', @$factory->fromGlobals()->getRemoteHost()); // deprecated
Assert::same('real', $factory->fromGlobals()->getUrl()->getHost());

$factory->setProxy(['10.0.0.1', '10.0.0.2']);
Assert::same('172.16.0.1', $factory->fromGlobals()->getRemoteAddress());
Assert::same('172.16.0.1', $factory->fromGlobals()->getRemoteHost());
Assert::same('172.16.0.1', @$factory->fromGlobals()->getRemoteHost()); // deprecated
Assert::same('real', $factory->fromGlobals()->getUrl()->getHost());
});

0 comments on commit e5b7619

Please sign in to comment.