Skip to content

Commit 8f65d9a

Browse files
authored
Merge pull request #1 from skoro/phpstan
Phpstan integration
2 parents f08edb4 + ef6c418 commit 8f65d9a

33 files changed

+217
-84
lines changed

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
}
2626
},
2727
"require-dev": {
28-
"phpunit/phpunit": "^9.5"
28+
"phpunit/phpunit": "^9.5",
29+
"phpstan/phpstan": "^1.3"
2930
},
3031
"autoload-dev": {
3132
"psr-4": {
@@ -35,6 +36,7 @@
3536
"scripts": {
3637
"test": "phpunit",
3738
"test-f": "phpunit --filter",
38-
"make-color-names": "@php make-color-names.php colors.txt > ./src/ColorNames.php"
39+
"make-color-names": "@php make-color-names.php colors.txt > ./src/ColorNames.php",
40+
"check": "phpstan"
3941
}
4042
}

composer.lock

Lines changed: 66 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpstan.neon.dist

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
parameters:
2+
level: 4
3+
paths:
4+
- src
5+
- tests
6+
7+
ignoreErrors:
8+
- '#Call to an undefined method FFI::Tcl_#'
9+
- '#Call to an undefined method FFI::Tk_#'

src/Color.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ class Color
1313
private int $green;
1414
private int $blue;
1515

16-
public function __construct(string $hex)
16+
final public function __construct(string $hex)
1717
{
1818
$this->extractColors($hex);
1919
}
2020

2121
protected function extractColors(string $hex)
2222
{
23-
list ($red, $green, $blue) = sscanf($hex, '#%02x%02x%02x');
23+
[$red, $green, $blue] = sscanf($hex, '#%02x%02x%02x');
2424
if ($red !== null && $green !== null && $blue !== null) {
2525
$this->red = $red;
2626
$this->green = $green;
@@ -35,7 +35,7 @@ protected static function assertUnsignedByte(int $value): int
3535
if ($value >= 0 && $value <= 255) {
3636
return $value;
3737
}
38-
throw new InvalidArgumentException('Argument must be unsigned byte.');
38+
throw new InvalidArgumentException(sprintf('Argument must be unsigned byte but got: %d', $value));
3939
}
4040

4141
public static function fromRgb(int $red, int $green, int $blue): self

src/Dialogs/Dialog.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ abstract class Dialog implements ModalWindow
1717
private Options $options;
1818
private Window $parent;
1919

20-
/** @var callable */
21-
private $callbackSuccess;
22-
/** @var callable */
23-
private $callbackCancel;
20+
/** @var callable|null */
21+
private $callbackSuccess = null;
22+
/** @var callable|null */
23+
private $callbackCancel = null;
2424

2525
public function __construct(Window $parent, array $options = [])
2626
{
@@ -59,6 +59,7 @@ public function __set($name, $value)
5959
throw new TkException('Parent must be a Window instance.');
6060
}
6161
$this->parent = $value;
62+
/** @phpstan-ignore-next-line */
6263
$this->options->parent = $value->path();
6364
break;
6465

src/DotEnv.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/**
99
* .env file environment loader.
1010
*/
11-
class DotEnv implements Environment
11+
final class DotEnv implements Environment
1212
{
1313
private array $data;
1414
private string $path;
@@ -55,7 +55,7 @@ public function load(): void
5555
public function loadAndMergeWith(array $override): void
5656
{
5757
$this->load();
58-
array_merge($this->data, $override);
58+
$this->data = array_merge($this->data, $override);
5959
}
6060

6161
/**

src/Font.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Font implements SplSubject
2020
/** @var SplObserver[] */
2121
private array $observers;
2222

23-
/** @var bool[string] */
23+
/** @var array<string, bool> */
2424
private array $styles;
2525

2626
public function __construct(string $name, int $size, ...$styles)
@@ -35,7 +35,7 @@ public function __construct(string $name, int $size, ...$styles)
3535
}
3636

3737
/**
38-
* @return bool[string]
38+
* @return array<string, bool>
3939
*/
4040
protected function defaultStyles(): array
4141
{
@@ -87,7 +87,7 @@ public function setSize(int $size): self
8787
}
8888

8989
/**
90-
* @return bool[string]
90+
* @return array<string, bool>
9191
*/
9292
public function getStyles(): array
9393
{
@@ -186,11 +186,17 @@ public function attach(SplObserver $observer): void
186186
$this->observers[] = $observer;
187187
}
188188

189-
public function detach(SplObserver $observer)
189+
public function detach(SplObserver $observer): void
190190
{
191191
$index = array_search($observer, $this->observers, true);
192192
if ($index !== false) {
193193
unset($this->observers[$index]);
194194
}
195195
}
196+
197+
// TODO: php8 Stringable interface
198+
public function __toString(): string
199+
{
200+
return sprintf('%s %d', $this->name, $this->size);
201+
}
196202
}

src/Options.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@ class Options
1313
{
1414
private array $options = [];
1515

16-
public function __construct(array $options = [])
16+
final public function __construct(array $options = [])
1717
{
18-
$this->options = $options;
18+
$this->mergeAsArray($this->defaults(), $options);
19+
}
20+
21+
protected function defaults(): array
22+
{
23+
return [];
1924
}
2025

2126
/**
@@ -124,9 +129,9 @@ public function merge(Options $options): self
124129
/**
125130
* Merge options from an array.
126131
*/
127-
public function mergeAsArray(array $options): self
132+
public function mergeAsArray(array ...$options): self
128133
{
129-
$this->options = array_merge($this->options, $options);
134+
$this->options = array_merge($this->options, ...$options);
130135
return $this;
131136
}
132137

src/TclTk/TkApplication.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ class TkApplication implements Application
4848
public function __construct(Tk $tk)
4949
{
5050
$this->tk = $tk;
51-
$this->ttkEnabled = false;
5251
$this->interp = $tk->interp();
5352
$this->bindings = $this->createBindings();
5453
$this->themeManager = null;
@@ -64,7 +63,7 @@ protected function createBindings(): Bindings
6463
return new TkBindings($this->interp);
6564
}
6665

67-
protected function createFontManager(): FontManager
66+
protected function createFontManager(): TkFontManager
6867
{
6968
return new TkFontManager($this->interp);
7069
}

src/TclTk/TkFont.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use PhpGui\Font;
66

7-
class TkFont extends Font
7+
final class TkFont extends Font
88
{
99
public function __toString(): string
1010
{
@@ -31,4 +31,14 @@ protected function getStyleNames(): array
3131
{
3232
return array_keys(array_filter($this->getStyles()));
3333
}
34+
35+
public static function createFromFontOptions(TkFontOptions $fontOptions): self
36+
{
37+
$font = new static($fontOptions->family, (int) $fontOptions->size);
38+
$font->setBold($fontOptions->weight === 'bold')
39+
->setItalic($fontOptions->slant === 'italic')
40+
->setUnderline((bool) $fontOptions->underline)
41+
->setOverstrike((bool) $fontOptions->overstrike);
42+
return $font;
43+
}
3444
}

0 commit comments

Comments
 (0)