Skip to content

Commit

Permalink
PHPStan level 7
Browse files Browse the repository at this point in the history
We add proper error handling, except for CodeStore, which we will
address in due course.
  • Loading branch information
cmb69 committed Mar 23, 2023
1 parent 7a2b98e commit dbb2ee8
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 25 deletions.
17 changes: 13 additions & 4 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,19 @@
</target>

<target name="stan" description="run static analysis">
<exec executable="phpstan" passthru="true" checkreturn="true">
<arg value="--ansi"/>
<arg value="analyze"/>
</exec>
<if>
<versioncompare version="${php.version}" desiredVersion="8.0.0" operator="ge"/>
<then>
<exec executable="phpstan" passthru="true" checkreturn="true">
<arg line="--ansi analyze"/>
</exec>
</then>
<else>
<exec executable="phpstan" passthru="true" checkreturn="true">
<arg line="--ansi -cphpstan7.neon analyze"/>
</exec>
</else>
</if>
</target>

<target name="test" description="runs all developer tests">
Expand Down
11 changes: 8 additions & 3 deletions classes/infra/CodeStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,12 @@ private function isExpired(array $record): bool
/** @return void */
private function begin(bool $exclusive)
{
$this->stream = fopen($this->filename, "c+");
$stream = fopen($this->filename, "c+");
assert($stream !== false);
$this->stream = $stream;
flock($this->stream, $exclusive ? LOCK_EX : LOCK_SH);
$bytes = fread($this->stream, self::HEADER_SIZE);
assert($bytes !== false);
if (strlen($bytes) < self::HEADER_SIZE) {
$bytes = pack("VV", self::START_SIZE, 0);
fwrite($this->stream, $bytes);
Expand All @@ -211,8 +214,10 @@ private function commit()
private function readRecord(int $slot): array
{
fseek($this->stream, self::HEADER_SIZE + $slot * self::RECORD_SIZE);
$result = unpack("coccupied/a15key/Vtimestamp/a12code", fread($this->stream, self::RECORD_SIZE));
assert(is_array($result));
$data = fread($this->stream, self::RECORD_SIZE);
assert($data !== false);
$result = unpack("coccupied/a15key/Vtimestamp/a12code", $data);
assert(is_array($result) && isset($result["occupied"]) && isset($result["key"]) && isset($result["timestamp"]));
$result["occupied"] = (bool) $result["occupied"];
$result["code"] = rtrim($result["code"], "\0");
return $result;
Expand Down
2 changes: 1 addition & 1 deletion classes/infra/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function render(string $_template, array $_data): string
extract($_data);
ob_start();
include $this->templateFolder . $_template . ".php";
return ob_get_clean();
return (string) ob_get_clean();
}

public function renderScript(string $filename): string
Expand Down
30 changes: 21 additions & 9 deletions classes/infra/VisualCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

class VisualCaptcha
{
/** @var resource|GdImage */
/** @var GdImage */
private $image;

/** @var array<Char> */
Expand Down Expand Up @@ -94,7 +94,7 @@ public function createImage(string $code)
}
ob_start();
imagepng($this->image);
return ob_get_clean();
return (string) ob_get_clean();
}

/** @return void */
Expand Down Expand Up @@ -146,7 +146,7 @@ private function precalculate()
imagedestroy($image);
}

/** @param resource|GdImage $image */
/** @param GdImage $image */
private function calculateTextWidth($image, int $blank): int
{
$width = (int) $this->config['crypt_width'];
Expand All @@ -161,7 +161,7 @@ private function calculateTextWidth($image, int $blank): int
return $xend - $xbegin;
}

/** @param resource|GdImage $image */
/** @param GdImage $image */
private function scanColumn($image, int $x, int $blank): int
{
for ($y = 0; $y < $this->config['crypt_height']; $y++) {
Expand All @@ -178,7 +178,11 @@ private function findBackgroundImage()
if ($this->config['bg_image']) {
$filename = $this->imageFolder . $this->config['bg_image'];
if (is_dir($filename)) {
$files = array_values(array_filter(scandir($filename), function ($basename) {
$entries = scandir($filename);
if ($entries === false) {
return false;
}
$files = array_values(array_filter($entries, function ($basename) {
return preg_match('/\.(gif|jpg|png)$/', $basename);
}));
return $filename . '/' . $files[$this->randomBackgroundImage(count($files))];
Expand Down Expand Up @@ -208,7 +212,12 @@ private function paintBackground()
{
$bgimg = $this->findBackgroundImage();
if ($bgimg) {
list($getwidth, $getheight, $gettype) = getimagesize($bgimg);
$imagesize = getimagesize($bgimg);
if ($imagesize === false) {
$this->paintStaticBackground();
return;
}
[$getwidth, $getheight, $gettype] = $imagesize;
switch ($gettype) {
case IMAGETYPE_GIF:
$imgread = imagecreatefromgif($bgimg);
Expand All @@ -220,8 +229,11 @@ private function paintBackground()
$imgread = imagecreatefrompng($bgimg);
break;
default:
$this->paintStaticBackground();
return;
$imgread = false;
}
if ($imgread === false) {
$this->paintStaticBackground();
return;
}
imagecopyresampled(
$this->image,
Expand Down Expand Up @@ -404,7 +416,7 @@ public function createErrorImage(string $text)
imagettftext($img, $fontsize, 0, $padding, $bbox[1]-$bbox[7]+1, $fg, $font, $text);
ob_start();
imagepng($img);
return ob_get_clean();
return (string) ob_get_clean();
}

/** @codeCoverageIgnore */
Expand Down
5 changes: 3 additions & 2 deletions classes/value/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static function from(string $url): self
/** @var string */
private $page;

/** @var array<string,string> */
/** @var array<string,string|array<string>> */
private $params;

/** @param string|array<string> $value */
Expand All @@ -64,7 +64,8 @@ public function without(string $param): self
return $that;
}

public function param(string $key): ?string
/** @return string|array<string>|null */
public function param(string $key)
{
return $this->params[$key] ?? null;
}
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 6
level: 7
paths:
- classes/
- views
Expand Down
5 changes: 0 additions & 5 deletions phpstan.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
<?php

const CMSIMPLE_XH_VERSION = 'CMSimple_XH 1.7.4';

const CMSIMPLE_URL = 'http://example.com/';

if (!class_exists('GdImage')) {
class GdImage {}
}
5 changes: 5 additions & 0 deletions phpstan7.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
includes:
- phpstan.neon
parameters:
typeAliases:
GdImage: resource

0 comments on commit dbb2ee8

Please sign in to comment.