Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/FileSystem/FileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ final class FileSystem implements FileSystemInterface
/**
* {@inheritdoc}
*/
public function open($path, $mode)
public function open(string $path, string $mode)
{
$stream = @fopen($path, $mode);

Expand All @@ -21,7 +21,7 @@ public function open($path, $mode)
/**
* {@inheritdoc}
*/
public function getContents($path)
public function getContents(string $path): string
{
$stream = $this->open($path, 'r');

Expand All @@ -36,7 +36,7 @@ public function getContents($path)
/**
* {@inheritdoc}
*/
public function putContents($path, $buffer)
public function putContents(string $path, string $buffer): int
{
$stream = $this->open($path, 'w');

Expand All @@ -59,15 +59,15 @@ private function exceptionIfFalse($result)
/**
* {@inheritdoc}
*/
public function exists($path)
public function exists(string $path): bool
{
return file_exists($path);
}

/**
* {@inheritdoc}
*/
public function isDir($path)
public function isDir(string $path): bool
{
return is_dir($path);
}
Expand Down
10 changes: 5 additions & 5 deletions src/FileSystem/FileSystemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface FileSystemInterface
*
* @return bool true if file exists, false otherwise
*/
public function exists($path);
public function exists(string $path): bool;

/**
* Tells whether the filename is a directory
Expand All @@ -20,7 +20,7 @@ public function exists($path);
*
* @return bool true if the filename exists and is a directory, false otherwise
*/
public function isDir($path);
public function isDir(string $path): bool;

/**
* Open a file.
Expand All @@ -30,7 +30,7 @@ public function isDir($path);
*
* @return resource A stream resource.
*/
public function open($path, $mode);
public function open(string $path, string $mode);

/**
* Read the contents of a file.
Expand All @@ -39,7 +39,7 @@ public function open($path, $mode);
*
* @return string The file contents
*/
public function getContents($path);
public function getContents(string $path): string;

/**
* Write a buffer to a file.
Expand All @@ -49,5 +49,5 @@ public function getContents($path);
*
* @return int The number of bytes written
*/
public function putContents($path, $buffer);
public function putContents(string $path, string $buffer): int;
}
9 changes: 6 additions & 3 deletions src/GPIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use PiPHP\GPIO\FileSystem\FileSystem;
use PiPHP\GPIO\FileSystem\FileSystemInterface;
use PiPHP\GPIO\Interrupt\InterruptWatcher;
use PiPHP\GPIO\Interrupt\InterruptWatcherInterface;
use PiPHP\GPIO\Pin\InputPinInterface;
use PiPHP\GPIO\Pin\OutputPinInterface;
use PiPHP\GPIO\Pin\Pin;
use PiPHP\GPIO\Pin\InputPin;
use PiPHP\GPIO\Pin\OutputPin;
Expand All @@ -29,15 +32,15 @@ public function __construct(FileSystemInterface $fileSystem = null, callable $st
/**
* {@inheritdoc}
*/
public function getInputPin($number)
public function getInputPin(int $number): InputPinInterface
{
return new InputPin($this->fileSystem, $number);
}

/**
* {@inheritdoc}
*/
public function getOutputPin($number, $exportDirection = Pin::DIRECTION_OUT)
public function getOutputPin($number, $exportDirection = Pin::DIRECTION_OUT): OutputPinInterface
{
if ($exportDirection !== Pin::DIRECTION_OUT && $exportDirection !== Pin::DIRECTION_LOW && $exportDirection !== Pin::DIRECTION_HIGH) {
throw new \InvalidArgumentException('exportDirection has to be an OUT type (OUT/LOW/HIGH).');
Expand All @@ -49,7 +52,7 @@ public function getOutputPin($number, $exportDirection = Pin::DIRECTION_OUT)
/**
* {@inheritdoc}
*/
public function createWatcher()
public function createWatcher(): InterruptWatcherInterface
{
return new InterruptWatcher($this->fileSystem, $this->streamSelect);
}
Expand Down
6 changes: 3 additions & 3 deletions src/GPIOInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface GPIOInterface
*
* @return InputPinInterface
*/
public function getInputPin($number);
public function getInputPin(int $number): InputPinInterface;

/**
* Get an output pin.
Expand All @@ -24,12 +24,12 @@ public function getInputPin($number);
*
* @return OutputPinInterface
*/
public function getOutputPin($number);
public function getOutputPin(int $number): OutputPinInterface;

/**
* Create an interrupt watcher.
*
* @return InterruptWatcherInterface
*/
public function createWatcher();
public function createWatcher(): InterruptWatcherInterface;
}
2 changes: 1 addition & 1 deletion src/Interrupt/InterruptWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function unregister(InputPinInterface $pin)
/**
* {@inheritdoc}
*/
public function watch($timeout)
public function watch(int $timeout): bool
{
$seconds = floor($timeout / 1000);
$carry = $timeout - ($seconds * 1000);
Expand Down
2 changes: 1 addition & 1 deletion src/Interrupt/InterruptWatcherInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ public function unregister(InputPinInterface $pin);
*
* @param int $timeout The maximum time to watch for in milliseconds.
*/
public function watch($timeout);
public function watch(int $timeout): bool;
}
4 changes: 2 additions & 2 deletions src/Pin/InputPin.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(FileSystemInterface $fileSystem, $number)
/**
* {@inheritdoc}
*/
public function getEdge()
public function getEdge(): string
{
$edgeFile = $this->getPinFile(self::GPIO_PIN_FILE_EDGE);
return trim($this->fileSystem->getContents($edgeFile));
Expand All @@ -33,7 +33,7 @@ public function getEdge()
/**
* {@inheritdoc}
*/
public function setEdge($edge)
public function setEdge(string $edge)
{
$edgeFile = $this->getPinFile(self::GPIO_PIN_FILE_EDGE);
$this->fileSystem->putContents($edgeFile, $edge);
Expand Down
4 changes: 2 additions & 2 deletions src/Pin/InputPinInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ interface InputPinInterface extends PinInterface
*
* @return string The pin edge value
*/
public function getEdge();
public function getEdge(): string;

/**
* Set the pin edge.
*
* @param string $edge The pin edge value to set
*/
public function setEdge($edge);
public function setEdge(string $edge);
}
2 changes: 1 addition & 1 deletion src/Pin/OutputPin.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(FileSystemInterface $fileSystem, $number, $exportDir
/**
* {@inheritdoc}
*/
public function setValue($value)
public function setValue(int $value)
{
$valueFile = $this->getPinFile(self::GPIO_PIN_FILE_VALUE);
$this->fileSystem->putContents($valueFile, $value);
Expand Down
2 changes: 1 addition & 1 deletion src/Pin/OutputPinInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ interface OutputPinInterface extends PinInterface
*
* @param int $value The value to set
*/
public function setValue($value);
public function setValue(int $value);
}
20 changes: 10 additions & 10 deletions src/Pin/Pin.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ abstract class Pin implements PinInterface
* @param FileSystemInterface $fileSystem An object that provides file system access
* @param int $number The number of the pin
*/
public function __construct(FileSystemInterface $fileSystem, $number)
public function __construct(FileSystemInterface $fileSystem, int $number)
{
$this->fileSystem = $fileSystem;
$this->number = $number;
Expand All @@ -46,7 +46,7 @@ public function __construct(FileSystemInterface $fileSystem, $number)
/**
* {@inheritdoc}
*/
public function getNumber()
public function getNumber(): int
{
return $this->number;
}
Expand Down Expand Up @@ -79,7 +79,7 @@ public function unexport()
/**
* {@inheritdoc}
*/
protected function isExported()
protected function isExported(): bool
{
$directory = $this->getPinDirectory();

Expand All @@ -89,7 +89,7 @@ protected function isExported()
/**
* {@inheritdoc}
*/
protected function getDirection()
protected function getDirection(): ?string
{
$directionFile = $this->getPinFile(self::GPIO_PIN_FILE_DIRECTION);

Expand All @@ -103,7 +103,7 @@ protected function getDirection()
/**
* {@inheritdoc}
*/
protected function setDirection($direction)
protected function setDirection(string $direction)
{
if ($this->getDirection() !== $direction) {
$directionFile = $this->getPinFile(self::GPIO_PIN_FILE_DIRECTION);
Expand All @@ -115,7 +115,7 @@ protected function setDirection($direction)
/**
* {@inheritdoc}
*/
public function getValue()
public function getValue(): int
{
$valueFile = $this->getPinFile(self::GPIO_PIN_FILE_VALUE);
return (int) trim($this->fileSystem->getContents($valueFile));
Expand All @@ -128,7 +128,7 @@ public function getValue()
*
* @return string The file path
*/
private function getFile($file)
private function getFile(string $file): string
{
return self::GPIO_PATH . $file;
}
Expand All @@ -138,7 +138,7 @@ private function getFile($file)
*
* @return string
*/
protected function getPinDirectory()
protected function getPinDirectory(): string
{
return self::GPIO_PATH . self::GPIO_PREFIX . $this->getNumber();
}
Expand All @@ -150,7 +150,7 @@ protected function getPinDirectory()
*
* @return string
*/
protected function getPinFile($file)
protected function getPinFile(string $file): string
{
return $this->getPinDirectory() . '/' . $file;
}
Expand All @@ -160,7 +160,7 @@ protected function getPinFile($file)
*
* @param string $file The file to write to
*/
private function writePinNumberToFile($file)
private function writePinNumberToFile(string $file)
{
$this->fileSystem->putContents($file, $this->getNumber());
}
Expand Down
4 changes: 2 additions & 2 deletions src/Pin/PinInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface PinInterface
*
* @return int
*/
public function getNumber();
public function getNumber(): int;

/**
* Export the pin.
Expand All @@ -29,5 +29,5 @@ public function unexport();
*
* @return int
*/
public function getValue();
public function getValue(): int;
}
11 changes: 6 additions & 5 deletions test/FileSystem/VFS.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@ class VFS implements FileSystemInterface
{
private $vfs = [];

public function open($path, $mode)
public function open(string $path, string $mode)
{
}

public function getContents($path)
public function getContents(string $path): string
{
return $this->vfs[$path];
}

public function putContents($path, $buffer)
public function putContents(string $path, string $buffer): int
{
$this->vfs[$path] = $buffer;
return 0;
}

public function exists($path)
public function exists(string $path): bool
{
$regex = sprintf('#^%s(\/.+)?$#', preg_quote($path, '#'));

Expand All @@ -31,7 +32,7 @@ public function exists($path)
}));
}

public function isDir($path)
public function isDir(string $path): bool
{
return $this->exists($path) && !array_key_exists($path, $this->vfs);
}
Expand Down