diff --git a/src/FileSystem/FileSystem.php b/src/FileSystem/FileSystem.php index 5aabd7b..609aa43 100644 --- a/src/FileSystem/FileSystem.php +++ b/src/FileSystem/FileSystem.php @@ -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); @@ -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'); @@ -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'); @@ -59,7 +59,7 @@ private function exceptionIfFalse($result) /** * {@inheritdoc} */ - public function exists($path) + public function exists(string $path): bool { return file_exists($path); } @@ -67,7 +67,7 @@ public function exists($path) /** * {@inheritdoc} */ - public function isDir($path) + public function isDir(string $path): bool { return is_dir($path); } diff --git a/src/FileSystem/FileSystemInterface.php b/src/FileSystem/FileSystemInterface.php index 366cd68..c701390 100644 --- a/src/FileSystem/FileSystemInterface.php +++ b/src/FileSystem/FileSystemInterface.php @@ -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 @@ -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. @@ -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. @@ -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. @@ -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; } diff --git a/src/GPIO.php b/src/GPIO.php index cbb3558..ab33ac5 100644 --- a/src/GPIO.php +++ b/src/GPIO.php @@ -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; @@ -29,7 +32,7 @@ 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); } @@ -37,7 +40,7 @@ public function getInputPin($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).'); @@ -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); } diff --git a/src/GPIOInterface.php b/src/GPIOInterface.php index ac7ac63..737b37a 100644 --- a/src/GPIOInterface.php +++ b/src/GPIOInterface.php @@ -15,7 +15,7 @@ interface GPIOInterface * * @return InputPinInterface */ - public function getInputPin($number); + public function getInputPin(int $number): InputPinInterface; /** * Get an output pin. @@ -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; } diff --git a/src/Interrupt/InterruptWatcher.php b/src/Interrupt/InterruptWatcher.php index 88265ab..d5bb08d 100644 --- a/src/Interrupt/InterruptWatcher.php +++ b/src/Interrupt/InterruptWatcher.php @@ -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); diff --git a/src/Interrupt/InterruptWatcherInterface.php b/src/Interrupt/InterruptWatcherInterface.php index c802934..e3f6931 100644 --- a/src/Interrupt/InterruptWatcherInterface.php +++ b/src/Interrupt/InterruptWatcherInterface.php @@ -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; } diff --git a/src/Pin/InputPin.php b/src/Pin/InputPin.php index e8d2eee..b046242 100644 --- a/src/Pin/InputPin.php +++ b/src/Pin/InputPin.php @@ -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)); @@ -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); diff --git a/src/Pin/InputPinInterface.php b/src/Pin/InputPinInterface.php index 49aba9f..c474ecb 100644 --- a/src/Pin/InputPinInterface.php +++ b/src/Pin/InputPinInterface.php @@ -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); } diff --git a/src/Pin/OutputPin.php b/src/Pin/OutputPin.php index 713940d..9ded559 100644 --- a/src/Pin/OutputPin.php +++ b/src/Pin/OutputPin.php @@ -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); diff --git a/src/Pin/OutputPinInterface.php b/src/Pin/OutputPinInterface.php index aa1a28f..92fdc48 100644 --- a/src/Pin/OutputPinInterface.php +++ b/src/Pin/OutputPinInterface.php @@ -9,5 +9,5 @@ interface OutputPinInterface extends PinInterface * * @param int $value The value to set */ - public function setValue($value); + public function setValue(int $value); } diff --git a/src/Pin/Pin.php b/src/Pin/Pin.php index c0926e8..993b7a3 100644 --- a/src/Pin/Pin.php +++ b/src/Pin/Pin.php @@ -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; @@ -46,7 +46,7 @@ public function __construct(FileSystemInterface $fileSystem, $number) /** * {@inheritdoc} */ - public function getNumber() + public function getNumber(): int { return $this->number; } @@ -79,7 +79,7 @@ public function unexport() /** * {@inheritdoc} */ - protected function isExported() + protected function isExported(): bool { $directory = $this->getPinDirectory(); @@ -89,7 +89,7 @@ protected function isExported() /** * {@inheritdoc} */ - protected function getDirection() + protected function getDirection(): ?string { $directionFile = $this->getPinFile(self::GPIO_PIN_FILE_DIRECTION); @@ -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); @@ -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)); @@ -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; } @@ -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(); } @@ -150,7 +150,7 @@ protected function getPinDirectory() * * @return string */ - protected function getPinFile($file) + protected function getPinFile(string $file): string { return $this->getPinDirectory() . '/' . $file; } @@ -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()); } diff --git a/src/Pin/PinInterface.php b/src/Pin/PinInterface.php index d8c2792..12a6281 100644 --- a/src/Pin/PinInterface.php +++ b/src/Pin/PinInterface.php @@ -12,7 +12,7 @@ interface PinInterface * * @return int */ - public function getNumber(); + public function getNumber(): int; /** * Export the pin. @@ -29,5 +29,5 @@ public function unexport(); * * @return int */ - public function getValue(); + public function getValue(): int; } diff --git a/test/FileSystem/VFS.php b/test/FileSystem/VFS.php index 57d9ceb..af1758e 100644 --- a/test/FileSystem/VFS.php +++ b/test/FileSystem/VFS.php @@ -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, '#')); @@ -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); }