Skip to content

Commit

Permalink
Merge pull request #315 from uzulla/refactoring-2021-05-23
Browse files Browse the repository at this point in the history
コードリファクタリング、型関連の厳格化
  • Loading branch information
fc2dev authored May 29, 2021
2 parents 2234a30 + caa8652 commit 2b150e0
Show file tree
Hide file tree
Showing 53 changed files with 448 additions and 465 deletions.
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

0 comments on commit 2b150e0

Please sign in to comment.