Skip to content

Commit 2b150e0

Browse files
authored
Merge pull request #315 from uzulla/refactoring-2021-05-23
コードリファクタリング、型関連の厳格化
2 parents 2234a30 + caa8652 commit 2b150e0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+448
-465
lines changed

app/src/App.php

+18-23
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
2-
/**
3-
* アプリ用の便利関数群
4-
*/
2+
declare(strict_types=1);
53

64
namespace Fc2blog;
75

@@ -15,7 +13,6 @@
1513

1614
class App
1715
{
18-
1916
/**
2017
* ブログIDから階層別フォルダ作成
2118
* @param string $blog_id
@@ -33,7 +30,7 @@ public static function getBlogLayer(string $blog_id): string
3330
* @param bool $timestamp
3431
* @return string
3532
*/
36-
public static function getUserFilePath(array $file, $abs = false, $timestamp = false): string
33+
public static function getUserFilePath(array $file, bool $abs = false, bool $timestamp = false): string
3734
{
3835
$file_path = static::getBlogLayer($file['blog_id']) . '/file/' . $file['id'] . '.' . $file['ext'];
3936
return ($abs ? Config::get('WWW_UPLOAD_DIR') : '/uploads/') . $file_path . ($timestamp ? '?t=' . strtotime($file['updated_at']) : '');
@@ -55,7 +52,7 @@ public static function getThumbnailPath(string $url, int $size = 72, string $whs
5552
if (!preg_match('{(/uploads/[0-9a-zA-Z]/[0-9a-zA-Z]/[0-9a-zA-Z]/[0-9a-zA-Z]+/file/[0-9]+)\.(png|gif|jpe?g)(\?t=[0-9]+)?$}', $url, $matches)) {
5653
return $url;
5754
}
58-
return $matches[1] . '_' . $whs . $size . '.' . $matches[2] . (isset($matches[3]) ? $matches[3] : '');
55+
return $matches[1] . '_' . $whs . $size . '.' . $matches[2] . ($matches[3] ?? '');
5956
}
6057

6158
/**
@@ -67,15 +64,15 @@ public static function getThumbnailPath(string $url, int $size = 72, string $whs
6764
* @param string $whs
6865
* @return string
6966
*/
70-
public static function getCenterThumbnailPath(string $url, $width = 760, $height = 420, $whs = ''): string
67+
public static function getCenterThumbnailPath(string $url, int $width = 760, int $height = 420, string $whs = ''): string
7168
{
7269
if (empty($url)) {
7370
return $url;
7471
}
7572
if (!preg_match('{(/uploads/[0-9a-zA-Z]/[0-9a-zA-Z]/[0-9a-zA-Z]/[0-9a-zA-Z]+/file/[0-9]+)\.(png|gif|jpe?g)(\?t=[0-9]+)?$}', $url, $matches)) {
7673
return $url;
7774
}
78-
return $matches[1] . '_' . $whs . $width . '_' . $height . '.' . $matches[2] . (isset($matches[3]) ? $matches[3] : '');
75+
return $matches[1] . '_' . $whs . $width . '_' . $height . '.' . $matches[2] . ($matches[3] ?? '');
7976
}
8077

8178
/**
@@ -105,14 +102,14 @@ public static function deleteFile(string $blog_id, string $id): void
105102
* @param string $id
106103
* @return string
107104
*/
108-
public static function getPluginFilePath(string $blog_id, string $id)
105+
public static function getPluginFilePath(string $blog_id, string $id): string
109106
{
110107
return Config::get('BLOG_TEMPLATE_DIR') . static::getBlogLayer($blog_id) . '/plugins/' . $id . '.php';
111108
}
112109

113110
/**
114111
* ファイルパスまでのフォルダを作成する
115-
* @param $file_path
112+
* @param string $file_path
116113
*/
117114
public static function mkdir(string $file_path): void
118115
{
@@ -167,7 +164,7 @@ public static function calcStartAndEndDate(int $year = 0, int $month = 0, int $d
167164
$end .= '12-31';
168165
}
169166
$dates = explode('-', $start);
170-
if (!checkdate($dates[1], $dates[2], $dates[0])) {
167+
if (!checkdate((int)$dates[1], (int)$dates[2], (int)$dates[0])) {
171168
// 存在日付の場合は本日を開始、終了日時として割り当てる
172169
$start = $end = date('Y-m-d');
173170
}
@@ -179,9 +176,9 @@ public static function calcStartAndEndDate(int $year = 0, int $month = 0, int $d
179176
/**
180177
* デバイスタイプを取得する
181178
* @param Request $request
182-
* @return string|null
179+
* @return int
183180
*/
184-
public static function getDeviceType(Request $request): string
181+
public static function getDeviceType(Request $request): int
185182
{
186183
// パラメータによりデバイスタイプを変更(FC2の引数順守)
187184
if ($request->isArgs('pc')) {
@@ -198,7 +195,7 @@ public static function getDeviceType(Request $request): string
198195
Config::get('DEVICE_SP'),
199196
];
200197
if (!empty($device_type) && in_array($device_type, $devices)) {
201-
return $device_type;
198+
return (int)$device_type;
202199
}
203200

204201
// ユーザーエージェントからデバイスタイプを取得
@@ -321,7 +318,7 @@ public static function userURL(Request $request, array $args = [], bool $reused
321318
$args = array_merge($gets, $args);
322319
}
323320

324-
$controller = $controller = $request->shortControllerName;
321+
$controller = $request->shortControllerName;
325322
if (isset($args['controller'])) {
326323
$controller = $args['controller'];
327324
unset($args['controller']);
@@ -366,8 +363,7 @@ public static function userURL(Request $request, array $args = [], bool $reused
366363
if ($blog_id && $blog_id !== Config::get('DEFAULT_BLOG_ID')) {
367364
$url = '/' . $blog_id . $url;
368365
}
369-
$url = ($abs ? $full_domain : '') . $url;
370-
return $url;
366+
return ($abs ? $full_domain : '') . $url;
371367
}
372368

373369
// 記事の場合
@@ -388,8 +384,7 @@ public static function userURL(Request $request, array $args = [], bool $reused
388384
if ($blog_id && $blog_id !== Config::get('DEFAULT_BLOG_ID')) {
389385
$url = '/' . $blog_id . $url;
390386
}
391-
$url = ($abs ? $full_domain : '') . $url;
392-
return $url;
387+
return ($abs ? $full_domain : '') . $url;
393388
}
394389

395390
$params = [];
@@ -409,14 +404,13 @@ public static function userURL(Request $request, array $args = [], bool $reused
409404
if ($blog_id && $blog_id !== Config::get('DEFAULT_BLOG_ID')) {
410405
$url = '/' . $blog_id . $url;
411406
}
412-
$url = ($abs ? $full_domain : '') . $url;
413-
return $url;
407+
return ($abs ? $full_domain : '') . $url;
414408
}
415409

416410
/**
417411
* ページ毎、デバイス毎の初期制限件数
418412
* @param Request $request
419-
* @param $key
413+
* @param string $key
420414
* @return int
421415
*/
422416
public static function getPageLimit(Request $request, string $key): int
@@ -427,7 +421,7 @@ public static function getPageLimit(Request $request, string $key): int
427421
/**
428422
* ページ毎、デバイス毎の件数一覧
429423
* @param Request $request
430-
* @param $key
424+
* @param string $key
431425
* @return array
432426
*/
433427
public static function getPageList(Request $request, string $key): array
@@ -441,6 +435,7 @@ public static function getPageList(Request $request, string $key): array
441435
* @param array|string params = array('entries/create', 'entries/edit', ...),
442436
* @return bool
443437
* TODO Configの削減
438+
* @noinspection PhpUnused
444439
*/
445440
public static function isActiveMenu(Request $request, $params): bool
446441
{

app/src/Model/ArrayIterableTrait.php

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Fc2blog\Model;
5+
6+
use ArrayIterator;
7+
use Iterator;
8+
use LogicException;
9+
use ReflectionClass;
10+
use ReflectionException;
11+
12+
trait ArrayIterableTrait
13+
{
14+
public static function factory(array $list): ?self
15+
{
16+
$self = new static();
17+
$props = (new ReflectionClass(static::class))->getProperties();
18+
foreach ($props as $prop) {
19+
$name = $prop->getName();
20+
$self->{$name} = $list[$name];
21+
}
22+
return $self;
23+
}
24+
25+
public function asArray(): array
26+
{
27+
return (array)$this;
28+
}
29+
30+
// ==== for array access ====
31+
public function offsetExists($offset): bool
32+
{
33+
return isset($this->{$offset});
34+
}
35+
36+
public function offsetGet($offset)
37+
{
38+
return $this->offsetExists($offset) ? $this->{$offset} : null;
39+
}
40+
41+
public function offsetSet($offset, $value)
42+
{
43+
$r = new ReflectionClass(static::class);
44+
try {
45+
$prop = $r->getProperty($offset);
46+
if ($prop->isPublic()) {
47+
$this->{$prop->getName()} = $value;
48+
}
49+
} catch (ReflectionException $e) {
50+
throw new LogicException("touch missing property " . $e->getMessage());
51+
}
52+
}
53+
54+
public function offsetUnset($offset)
55+
{
56+
throw new LogicException("un-settable.");
57+
}
58+
59+
public function getIterator(): Iterator
60+
{
61+
return new ArrayIterator((array)$this);
62+
}
63+
64+
public function count(): int
65+
{
66+
$r = new ReflectionClass(static::class);
67+
return count($r->getProperties());
68+
}
69+
}

app/src/Model/Blog.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Fc2blog\Model;
5+
6+
use ArrayAccess;
7+
use Countable;
8+
use IteratorAggregate;
9+
10+
class Blog implements ArrayAccess, IteratorAggregate, Countable
11+
{
12+
use ArrayIterableTrait;
13+
14+
public $id;
15+
public $user_id;
16+
public $name;
17+
public $nickname;
18+
public $introduction;
19+
public $template_pc_id;
20+
public $template_sp_id;
21+
public $timezone;
22+
public $open_status;
23+
public $ssl_enable;
24+
public $redirect_status_code;
25+
public $blog_password;
26+
public $trip_salt;
27+
public $last_posted_at;
28+
public $created_at;
29+
public $updated_at;
30+
}

app/src/Model/BlogPluginsModel.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public function getAutoIncrementCompositeKey(): string
3636

3737
/**
3838
* バリデート処理
39-
* @param $data
40-
* @param $valid_data
39+
* @param array $data
40+
* @param array|null $valid_data
4141
* @param array $white_list
4242
* @return array
4343
*/

app/src/Model/BlogTemplatesModel.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public function getAutoIncrementCompositeKey(): string
3636

3737
/**
3838
* バリデート処理
39-
* @param $data
40-
* @param $valid_data
39+
* @param array $data
40+
* @param array|null $valid_data
4141
* @param array $white_list
4242
* @return array
4343
*/
@@ -69,7 +69,7 @@ public function validate(array $data, ?array &$valid_data = [], array $white_lis
6969

7070
/**
7171
* FC2テンプレートの構文チェック
72-
* @param $php_code
72+
* @param string $php_code
7373
* @return bool|string
7474
*/
7575
public static function fc2TemplateSyntax(string $php_code)
@@ -343,9 +343,7 @@ public static function convertFC2Template(string $html)
343343
// 変数タグの置き換え
344344
$html = str_replace(Config::get('fc2_template_var_search'), Config::get('fc2_template_var_replace'), $html);
345345
// 処理されなかった(対応がなかった)変数タグの削除
346-
$html = preg_replace('/<%[0-9a-zA-Z_]+?>/', '', $html);
347-
348-
return $html;
346+
return preg_replace('/<%[0-9a-zA-Z_]+?>/', '', $html);
349347
}
350348

351349
static public function getPathDefaultTemplate(): string

app/src/Model/BlogsModel.php

+12-11
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function getTableName(): string
3838
* @param $key
3939
* @param $data
4040
* @return bool|string
41+
* @noinspection PhpUnusedParameterInspection // $options and $key needs dynamic call
4142
*/
4243
public static function privateCheck($value, $option, $key, $data)
4344
{
@@ -60,7 +61,7 @@ public static function privateCheck($value, $option, $key, $data)
6061
* @param $blog_id
6162
* @return bool
6263
*/
63-
public static function isPasswordRegistered($blog_id)
64+
public static function isPasswordRegistered($blog_id): bool
6465
{
6566
$blog = (new BlogsModel)->findById($blog_id);
6667
return (!empty($blog) && strlen($blog['blog_password']) > 0);
@@ -87,8 +88,8 @@ public static function usableDirectory(string $value)
8788

8889
/**
8990
* バリデート処理
90-
* @param $data
91-
* @param $valid_data
91+
* @param array $data
92+
* @param array|null $valid_data
9293
* @param array $white_list
9394
* @return array
9495
*/
@@ -586,9 +587,9 @@ public function deleteByIdAndUserId($blog_id, int $user_id, $options = array())
586587

587588
/**
588589
* 指定したテンプレートIDが指定したブログIDのデバイステンプレートとして適用されているか判定する
589-
* @param $template_id
590-
* @param $blog_id
591-
* @param $device_type
590+
* @param int $template_id
591+
* @param string $blog_id
592+
* @param int $device_type
592593
* @return bool
593594
*/
594595
public function isAppliedTemplate(int $template_id, string $blog_id, int $device_type): bool
@@ -603,8 +604,8 @@ public function isAppliedTemplate(int $template_id, string $blog_id, int $device
603604

604605
/**
605606
* 指定したブログID,デバイスIDのテンプレートIDを取得する
606-
* @param $blog_id
607-
* @param $device_type
607+
* @param string $blog_id
608+
* @param int $device_type
608609
* @return int
609610
*/
610611
public function getAppliedTemplateId(string $blog_id, int $device_type): int
@@ -645,10 +646,10 @@ static public function isCorrectHttpSchemaByBlogId(Request $request, string $blo
645646
/**
646647
* エントリのパーマリンク
647648
* @param string $blog_id
648-
* @param string $entry_id
649+
* @param int $entry_id
649650
* @return string
650651
*/
651-
static public function getEntryFullUrlByBlogIdAndEntryId(string $blog_id, string $entry_id): string
652+
static public function getEntryFullUrlByBlogIdAndEntryId(string $blog_id, int $entry_id): string
652653
{
653654
$schema = static::getSchemaByBlogId($blog_id);
654655
$domain = Config::get("DOMAIN");
@@ -659,7 +660,7 @@ static public function getEntryFullUrlByBlogIdAndEntryId(string $blog_id, string
659660
} else {
660661
$blog_id_path = "";
661662
}
662-
return $schema . "//" . $domain . $port . $blog_id_path . "/blog-entry-" . (int)$entry_id . ".html";
663+
return $schema . "//" . $domain . $port . $blog_id_path . "/blog-entry-" . $entry_id . ".html";
663664
}
664665

665666
/**

app/src/Model/CategoriesModel.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public function getList($blog_id, $options = array())
161161
{
162162
$options['where'] = (isset($options['where']) && $options['where'] != '') ? 'blog_id=? AND ' . $options['where'] : 'blog_id=?';
163163
$options['params'] = isset($options['params']) ? array_merge(array($blog_id), $options['params']) : array($blog_id);
164-
$options['order'] = isset($options['order']) ? $options['order'] : 'categories.lft';
164+
$options['order'] = $options['order'] ?? 'categories.lft';
165165
return $this->findNode($options);
166166
}
167167

0 commit comments

Comments
 (0)