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 Dec 11, 2023
1 parent 4a56d39 commit c2abfd0
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Http/FileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,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 @@ -368,9 +368,10 @@ private function parseHostAndPort(string $s): ?array
}


/** @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 @@ -99,6 +99,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 @@ -110,6 +111,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();
if ($this->warnOnUndefined && !array_key_exists($name, $data ?? [])) {
Expand All @@ -126,6 +128,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 @@ -137,6 +140,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 @@ -147,6 +151,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 @@ -157,6 +162,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 @@ -167,6 +173,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 @@ -177,6 +184,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());
});

0 comments on commit c2abfd0

Please sign in to comment.