Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

コードリファクタリング、型関連の厳格化 #315

Merged
merged 28 commits into from
May 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
caba6b5
Refactoring, add declare(strict_types=1);. add type hint,definition. …
uzulla May 23, 2021
7c456eb
Refactoring Session class, add declare(strict_types=1);. add type.
uzulla May 23, 2021
53b4f66
Refactoring Cookie class, add declare(strict_types=1);. add type.
uzulla May 23, 2021
c74e728
Refactoring Request class, add declare(strict_types=1);. add type.
uzulla May 23, 2021
39be5ca
Refactoring Controller class.
uzulla May 23, 2021
a18c14d
move getHostUrl to Controller class from AdminController.
uzulla May 23, 2021
eadb0ec
Refactoring Blog,User classes and ArrayIterableTrait.
uzulla May 23, 2021
046b8f0
Refactoring
uzulla May 23, 2021
ec331f1
Refactoring, use BlogService.
uzulla May 23, 2021
411b7cf
bug fix caused by declare(strict_types=1);. change to explicit type c…
uzulla May 23, 2021
afea601
bug fix. add null check.
uzulla May 23, 2021
dc8d7fc
Fix to correct type.
uzulla May 23, 2021
7468a18
add declare(strict_types=1); and suppress some warn.
uzulla May 23, 2021
d7f33d3
Refactoring EntriesController
uzulla May 23, 2021
466ed84
Refactoring, Add type hint, suppress linter warn, remove unnecessary …
uzulla May 23, 2021
d79e1ea
Refactoring App class, Add declare(strict_types=1);.
uzulla May 23, 2021
b888443
align to correct type.
uzulla May 23, 2021
6fdfede
suppress linter warnings. disable check.
uzulla May 23, 2021
3bafa60
refactoring, update PhpDocs.
uzulla May 23, 2021
8ba908e
refactoring
uzulla May 23, 2021
66095ea
refactoring
uzulla May 23, 2021
b94ef66
refactoring
uzulla May 23, 2021
2f10d2e
refactoring
uzulla May 23, 2021
a8ed42c
add noinspection annotation to db libs.
uzulla May 23, 2021
cc1e460
add noinspection. too complex inline html building for linter.
uzulla May 23, 2021
afe37c5
Improve error handling when mail sending.
uzulla May 23, 2021
37c8e37
Suppress linter warn
uzulla May 23, 2021
caa8652
Suppress linter warn
uzulla May 23, 2021
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
41 changes: 18 additions & 23 deletions app/src/App.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php
/**
* アプリ用の便利関数群
*/
declare(strict_types=1);

namespace Fc2blog;

Expand All @@ -15,7 +13,6 @@

class App
{

/**
* ブログIDから階層別フォルダ作成
* @param string $blog_id
Expand All @@ -33,7 +30,7 @@ public static function getBlogLayer(string $blog_id): string
* @param bool $timestamp
* @return string
*/
public static function getUserFilePath(array $file, $abs = false, $timestamp = false): string
public static function getUserFilePath(array $file, bool $abs = false, bool $timestamp = false): string
{
$file_path = static::getBlogLayer($file['blog_id']) . '/file/' . $file['id'] . '.' . $file['ext'];
return ($abs ? Config::get('WWW_UPLOAD_DIR') : '/uploads/') . $file_path . ($timestamp ? '?t=' . strtotime($file['updated_at']) : '');
Expand All @@ -55,7 +52,7 @@ public static function getThumbnailPath(string $url, int $size = 72, string $whs
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)) {
return $url;
}
return $matches[1] . '_' . $whs . $size . '.' . $matches[2] . (isset($matches[3]) ? $matches[3] : '');
return $matches[1] . '_' . $whs . $size . '.' . $matches[2] . ($matches[3] ?? '');
}

/**
Expand All @@ -67,15 +64,15 @@ public static function getThumbnailPath(string $url, int $size = 72, string $whs
* @param string $whs
* @return string
*/
public static function getCenterThumbnailPath(string $url, $width = 760, $height = 420, $whs = ''): string
public static function getCenterThumbnailPath(string $url, int $width = 760, int $height = 420, string $whs = ''): string
{
if (empty($url)) {
return $url;
}
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)) {
return $url;
}
return $matches[1] . '_' . $whs . $width . '_' . $height . '.' . $matches[2] . (isset($matches[3]) ? $matches[3] : '');
return $matches[1] . '_' . $whs . $width . '_' . $height . '.' . $matches[2] . ($matches[3] ?? '');
}

/**
Expand Down Expand Up @@ -105,14 +102,14 @@ public static function deleteFile(string $blog_id, string $id): void
* @param string $id
* @return string
*/
public static function getPluginFilePath(string $blog_id, string $id)
public static function getPluginFilePath(string $blog_id, string $id): string
{
return Config::get('BLOG_TEMPLATE_DIR') . static::getBlogLayer($blog_id) . '/plugins/' . $id . '.php';
}

/**
* ファイルパスまでのフォルダを作成する
* @param $file_path
* @param string $file_path
*/
public static function mkdir(string $file_path): void
{
Expand Down Expand Up @@ -167,7 +164,7 @@ public static function calcStartAndEndDate(int $year = 0, int $month = 0, int $d
$end .= '12-31';
}
$dates = explode('-', $start);
if (!checkdate($dates[1], $dates[2], $dates[0])) {
if (!checkdate((int)$dates[1], (int)$dates[2], (int)$dates[0])) {
// 存在日付の場合は本日を開始、終了日時として割り当てる
$start = $end = date('Y-m-d');
}
Expand All @@ -179,9 +176,9 @@ public static function calcStartAndEndDate(int $year = 0, int $month = 0, int $d
/**
* デバイスタイプを取得する
* @param Request $request
* @return string|null
* @return int
*/
public static function getDeviceType(Request $request): string
public static function getDeviceType(Request $request): int
{
// パラメータによりデバイスタイプを変更(FC2の引数順守)
if ($request->isArgs('pc')) {
Expand All @@ -198,7 +195,7 @@ public static function getDeviceType(Request $request): string
Config::get('DEVICE_SP'),
];
if (!empty($device_type) && in_array($device_type, $devices)) {
return $device_type;
return (int)$device_type;
}

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

$controller = $controller = $request->shortControllerName;
$controller = $request->shortControllerName;
if (isset($args['controller'])) {
$controller = $args['controller'];
unset($args['controller']);
Expand Down Expand Up @@ -366,8 +363,7 @@ public static function userURL(Request $request, array $args = [], bool $reused
if ($blog_id && $blog_id !== Config::get('DEFAULT_BLOG_ID')) {
$url = '/' . $blog_id . $url;
}
$url = ($abs ? $full_domain : '') . $url;
return $url;
return ($abs ? $full_domain : '') . $url;
}

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

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

/**
* ページ毎、デバイス毎の初期制限件数
* @param Request $request
* @param $key
* @param string $key
* @return int
*/
public static function getPageLimit(Request $request, string $key): int
Expand All @@ -427,7 +421,7 @@ public static function getPageLimit(Request $request, string $key): int
/**
* ページ毎、デバイス毎の件数一覧
* @param Request $request
* @param $key
* @param string $key
* @return array
*/
public static function getPageList(Request $request, string $key): array
Expand All @@ -441,6 +435,7 @@ public static function getPageList(Request $request, string $key): array
* @param array|string params = array('entries/create', 'entries/edit', ...),
* @return bool
* TODO Configの削減
* @noinspection PhpUnused
*/
public static function isActiveMenu(Request $request, $params): bool
{
Expand Down
69 changes: 69 additions & 0 deletions app/src/Model/ArrayIterableTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
declare(strict_types=1);

namespace Fc2blog\Model;

use ArrayIterator;
use Iterator;
use LogicException;
use ReflectionClass;
use ReflectionException;

trait ArrayIterableTrait
{
public static function factory(array $list): ?self
{
$self = new static();
$props = (new ReflectionClass(static::class))->getProperties();
foreach ($props as $prop) {
$name = $prop->getName();
$self->{$name} = $list[$name];
}
return $self;
}

public function asArray(): array
{
return (array)$this;
}

// ==== for array access ====
public function offsetExists($offset): bool
{
return isset($this->{$offset});
}

public function offsetGet($offset)
{
return $this->offsetExists($offset) ? $this->{$offset} : null;
}

public function offsetSet($offset, $value)
{
$r = new ReflectionClass(static::class);
try {
$prop = $r->getProperty($offset);
if ($prop->isPublic()) {
$this->{$prop->getName()} = $value;
}
} catch (ReflectionException $e) {
throw new LogicException("touch missing property " . $e->getMessage());
}
}

public function offsetUnset($offset)
{
throw new LogicException("un-settable.");
}

public function getIterator(): Iterator
{
return new ArrayIterator((array)$this);
}

public function count(): int
{
$r = new ReflectionClass(static::class);
return count($r->getProperties());
}
}
30 changes: 30 additions & 0 deletions app/src/Model/Blog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);

namespace Fc2blog\Model;

use ArrayAccess;
use Countable;
use IteratorAggregate;

class Blog implements ArrayAccess, IteratorAggregate, Countable
{
use ArrayIterableTrait;

public $id;
public $user_id;
public $name;
public $nickname;
public $introduction;
public $template_pc_id;
public $template_sp_id;
public $timezone;
public $open_status;
public $ssl_enable;
public $redirect_status_code;
public $blog_password;
public $trip_salt;
public $last_posted_at;
public $created_at;
public $updated_at;
}
4 changes: 2 additions & 2 deletions app/src/Model/BlogPluginsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public function getAutoIncrementCompositeKey(): string

/**
* バリデート処理
* @param $data
* @param $valid_data
* @param array $data
* @param array|null $valid_data
* @param array $white_list
* @return array
*/
Expand Down
10 changes: 4 additions & 6 deletions app/src/Model/BlogTemplatesModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public function getAutoIncrementCompositeKey(): string

/**
* バリデート処理
* @param $data
* @param $valid_data
* @param array $data
* @param array|null $valid_data
* @param array $white_list
* @return array
*/
Expand Down Expand Up @@ -69,7 +69,7 @@ public function validate(array $data, ?array &$valid_data = [], array $white_lis

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

return $html;
return preg_replace('/<%[0-9a-zA-Z_]+?>/', '', $html);
}

static public function getPathDefaultTemplate(): string
Expand Down
23 changes: 12 additions & 11 deletions app/src/Model/BlogsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function getTableName(): string
* @param $key
* @param $data
* @return bool|string
* @noinspection PhpUnusedParameterInspection // $options and $key needs dynamic call
*/
public static function privateCheck($value, $option, $key, $data)
{
Expand All @@ -60,7 +61,7 @@ public static function privateCheck($value, $option, $key, $data)
* @param $blog_id
* @return bool
*/
public static function isPasswordRegistered($blog_id)
public static function isPasswordRegistered($blog_id): bool
{
$blog = (new BlogsModel)->findById($blog_id);
return (!empty($blog) && strlen($blog['blog_password']) > 0);
Expand All @@ -87,8 +88,8 @@ public static function usableDirectory(string $value)

/**
* バリデート処理
* @param $data
* @param $valid_data
* @param array $data
* @param array|null $valid_data
* @param array $white_list
* @return array
*/
Expand Down Expand Up @@ -586,9 +587,9 @@ public function deleteByIdAndUserId($blog_id, int $user_id, $options = array())

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

/**
* 指定したブログID,デバイスIDのテンプレートIDを取得する
* @param $blog_id
* @param $device_type
* @param string $blog_id
* @param int $device_type
* @return int
*/
public function getAppliedTemplateId(string $blog_id, int $device_type): int
Expand Down Expand Up @@ -645,10 +646,10 @@ static public function isCorrectHttpSchemaByBlogId(Request $request, string $blo
/**
* エントリのパーマリンク
* @param string $blog_id
* @param string $entry_id
* @param int $entry_id
* @return string
*/
static public function getEntryFullUrlByBlogIdAndEntryId(string $blog_id, string $entry_id): string
static public function getEntryFullUrlByBlogIdAndEntryId(string $blog_id, int $entry_id): string
{
$schema = static::getSchemaByBlogId($blog_id);
$domain = Config::get("DOMAIN");
Expand All @@ -659,7 +660,7 @@ static public function getEntryFullUrlByBlogIdAndEntryId(string $blog_id, string
} else {
$blog_id_path = "";
}
return $schema . "//" . $domain . $port . $blog_id_path . "/blog-entry-" . (int)$entry_id . ".html";
return $schema . "//" . $domain . $port . $blog_id_path . "/blog-entry-" . $entry_id . ".html";
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/src/Model/CategoriesModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public function getList($blog_id, $options = array())
{
$options['where'] = (isset($options['where']) && $options['where'] != '') ? 'blog_id=? AND ' . $options['where'] : 'blog_id=?';
$options['params'] = isset($options['params']) ? array_merge(array($blog_id), $options['params']) : array($blog_id);
$options['order'] = isset($options['order']) ? $options['order'] : 'categories.lft';
$options['order'] = $options['order'] ?? 'categories.lft';
return $this->findNode($options);
}

Expand Down
Loading