1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-12 09:19:40 +01:00

Add some notes

This commit is contained in:
Olle 2020-06-19 00:37:49 +00:00
parent 9d284f1331
commit 2efe135291

95
typestatetest.php Normal file
View File

@ -0,0 +1,95 @@
<?php
// Examples: Query builder, file, socket.
interface ClosedFile
{
public function open();
}
interface OpenFile
{
public function read();
public function close();
}
interface CannotOpen {}
class MyFile implements ClosedFile, OpenFile, CannotOpen
{
/**
* @self-out ClosedFile
*/
public function __construct() {
}
/**
* @self-out OpenFile|CannotOpen
*/
public function open() {
// if can't open, throw exception and invalidate $this
}
/**
* @self-out ClosedFile
*/
public function close() {
}
/**
* @return string
*/
public function read() {
return 'content';
}
}
$file = new MyFile("somefile");
$file->open();
if ($file instanceof OpenFile) {
$content = $file->read();
$file->close();
} elseif ($file instanceof CannotOpen) {
// Mooo.
}
for ($i = 1; $i < 10; ++$i) {
// TODO: What here?
$file->open();
}
// ClosedFile
$file = new MyFile("somefile");
if ($file instanceof OpenFile) {
// TODO: Impossible
}
if (hardcomputation()) {
$file->open();
}
// $file is OpenFile|ClosedFile|CannotOpen
// TODO: Have to check state?
/**
* @param OpenFile $file
*/
function readall(OpenFile $file) {
$file->read();
$file->close(); // TODO: Not allowed to change state of aliased variable.
return $file; // TODO: Not allowed to transfer ownership
}
/**
* @param string $name
* @return ClosedFile
*/
function makefile($name) {
// ClosedFile
$file = new File($name);
dosomething($file); // Can't change state, still closed
return $file; // Owner can change
}
/**
* @param ClosedFile $file
* @param-out OpenFile $file
*/
function open(ClosedFile &$file)
{
}