-
Notifications
You must be signed in to change notification settings - Fork 667
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Weird error regarding type closed-resource
#2763
Comments
I found these snippets: https://psalm.dev/r/8581e64e34<?php
class Socket {
/** @var resource|null */
protected $connection;
/** @var bool */
protected $connected = false;
/**
* Disconnect the socket from the current connection.
*
* @return bool Success
*/
public function disconnect(): bool
{
if (!is_resource($this->connection)) {
$this->connected = false;
return true;
}
$this->connected = !fclose($this->connection);
if (!$this->connected) {
$this->connection = null;
}
return !$this->connected;
}
}
|
closed-resource
closed-resource
So, PHP is fun. If you close a resource, it's not resource anymore – calling Therefore its property type Psalm doesn't warn when you close resources directly inside |
Yeah, hence the
Okay, I understand that technically it's no longer valid but IMHO this error is useless in most practically valid use cases. The currently code is perfectly valid.
That just ends up throwing more errors wherever the property is used. |
Yeah, I understand it's frustrating. The workaround (from Psalm's point of view) is to put the connection into a separate variable, close that, then assign the actual property to |
I found these snippets: https://psalm.dev/r/5961c19533<?php
class Socket {
/** @var resource|null */
protected $connection;
/** @var bool */
protected $connected = false;
/**
* Disconnect the socket from the current connection.
*
* @return bool Success
*/
public function disconnect(): bool
{
if (!is_resource($this->connection)) {
$this->connected = false;
return true;
}
$connection = $this->connection;
$this->connected = !fclose($connection);
if (!$this->connected) {
$this->connection = null;
}
return !$this->connected;
}
}
|
Hopefully someone will come up with a proper object-based replacement for |
Thanks, but I am just going to use |
I found these snippets: https://psalm.dev/r/5961c19533<?php
class Socket {
/** @var resource|null */
protected $connection;
/** @var bool */
protected $connected = false;
/**
* Disconnect the socket from the current connection.
*
* @return bool Success
*/
public function disconnect(): bool
{
if (!is_resource($this->connection)) {
$this->connected = false;
return true;
}
$connection = $this->connection;
$this->connected = !fclose($connection);
if (!$this->connected) {
$this->connection = null;
}
return !$this->connected;
}
}
|
Yeah, makes sense. FWIW I added the |
https://psalm.dev/r/8581e64e34
Actual code here https://github.com/cakephp/cakephp/blob/c46238303a3bd8a711c90c503dd1f12ba3656ebe/src/Network/Socket.php#L421
The text was updated successfully, but these errors were encountered: