PHPMDを使おう

まずtestコードを書きます。

class Test {

	private $t = 1;

	public function test(){
		echo $this->t;
		$test = 2;
	}
}

で、テストします。

[vagrant@localhost app]$ vendor/phpmd/phpmd/src/bin/phpmd test.php text unusedcode,naming
/home/vagrant/local/app/test.php:5 Avoid variables with short names like $t. Configured minimum length is 3.
/home/vagrant/local/app/test.php:7 Classes should not have a constructor method with the same name as the class
/home/vagrant/local/app/test.php:9 Avoid unused local variables such as ‘$test’.

ぎゃあああああああああああああああああああああ
修正します。

class Test {

	private $test1 = 1;

	public function index(){
		echo $this->test1;
		$test2 = 2;
		echo $test2;
	}
}

で、再度テストすると。。
[vagrant@localhost app]$ vendor/phpmd/phpmd/src/bin/phpmd test.php text unusedcode,naming

おおおおおおおおお、こういうことか~

proc_open(): fork failed – Cannot allocate memory

[vagrant@localhost test]$ sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 17.3872 s, 61.8 MB/s
[vagrant@localhost test]$ sudo /sbin/mkswap /var/swap.1
スワップ空間バージョン1を設定します、サイズ = 1048572 KiB
ラベルはありません, UUID=4b1bec65-8ded-4ff2-8ed8-49e83f909f09
[vagrant@localhost test]$ sudo /sbin/swapon /var/swap.1

[vagrant@localhost test]$ free
total used free shared buffers cached
Mem: 501796 493940 7856 4 1184 402464
-/+ buffers/cache: 90292 411504
Swap: 2064376 735636 1328740

で、再度いれます。
php composer.phar require “phpmd/phpmd=~2.0”
今度は上手くいきそうです。
入りましたかね。

phpmdを使おう

composerを入れます。

[vagrant@localhost test]$ curl -sS https://getcomposer.org/installer | php
All settings correct for using Composer
Downloading…

Composer (version 1.8.6) successfully installed to: /home/vagrant/local/app/test/test/composer.phar
Use it: php composer.phar

[vagrant@localhost test]$ ls
composer.phar

https://phpmd.org/download/releases/index.html
あれ、アクセスできない??
とりあえず2.0を入れてみます。
# php composer.phar require “phpmd/phpmd=~2.0”

phpmd/phpmd

composer require phpmd/phpmd

What is phpmd?
->It points out potential bugs, suboptimal code, complex expressions, unused parameters, methods, and properties.

About rulesets
The rule set that can be sepecified by PHPMD is as follows.

Clean code rule: cleancode
will point out about the dirty code.

Code size rule: codesize
Attempts to reduce code size by splitting if there are too many methods or large classes.

Discussion rule: controversial
Point out to use the camel case about the camel case and snake case that are often regarded as a problem in developing with PHP

Design rules:
Design issues that are related to software design.

Naming rule: naming
Point out long names, conversely short names etc.

Unused code rule: unusedcode
Find and point out unused code.

laravelcollective

composer require laravelcollective/html

む? なんだ、laravelcollectiveとは?
https://laravelcollective.com/

>セレクトボックスの組み立てやチェックボックスの初期状態の指定、CSRF対策のトークン埋め込みなどが簡単にできる
う~ん、使うのか?必要性があれば使いますかね。

linux cp command

[vagrant@localhost test]$ ls
[vagrant@localhost test]$ touch fileA.txt
[vagrant@localhost test]$ ls
fileA.txt
[vagrant@localhost test]$ cp fileA.txt fileA_copy.txt
[vagrant@localhost test]$ ls
fileA.txt fileA_copy.txt

ディレクトリ先にもコピーできる。
[vagrant@localhost test]$ mkdir dir
[vagrant@localhost test]$ ls
dir fileA.txt fileA_copy.txt
[vagrant@localhost test]$ cp fileA.txt dir/fileA_cp.txt
[vagrant@localhost test]$ cd dir
[vagrant@localhost dir]$ ls
fileA_cp.txt

cpコマンドオプション -rでディレクトリごとコピー
[vagrant@localhost test]$ ls
dir fileA.txt fileA_copy.txt
[vagrant@localhost test]$ cp -r dir dir2
[vagrant@localhost test]$ ls
dir dir2 fileA.txt fileA_copy.txt
[vagrant@localhost test]$ cd dir2
[vagrant@localhost dir2]$ ls
fileA_cp.txt

-f:コピー先に同じ名前のファイルがあっても警告なしで上書きをする。
[vagrant@localhost test]$ ls
dir dir2 fileA.txt fileA_copy.txt
[vagrant@localhost test]$ cp -f fileA.txt fileA_copy.txt
[vagrant@localhost test]$ ls
dir dir2 fileA.txt fileA_copy.txt
[vagrant@localhost test]$ cp -rf dir dir2
[vagrant@localhost test]$ ls
dir dir2 fileA.txt fileA_copy.txt

なるほど、ディレクトリのコピーはcp -rfが良さそうですな。

MySQL: LPAD(特定)の文字を左側に追加

LPAD関数を使用することで、第二引数で指定した文字数に達するまで文字列1の左側に文字列2を追加する

mysql> SELECT LPAD(‘1′,3,’0’);
+—————–+
| LPAD(‘1′,3,’0’) |
+—————–+
| 001 |
+—————–+
1 row in set (9.23 sec)

なるほど、文字列作るとき便利だなーこれ。

MySQL: 文字列を結合するCONCAT

mysql> SELECT CONCAT(‘My’,’SQL’,’関数’,’リファレンス’);
+————————————————–+
| CONCAT(‘My’,’SQL’,’関数’,’リファレンス’) |
+————————————————–+
| MySQL関数リファレンス |
+————————————————–+
1 row in set (0.83 sec)

nullが入っているとnullで返されるらしいんだが。。
mysql> SELECT CONCAT(‘My’,’SQL’,’NULL’,’リファレンス’);
+————————————————+
| CONCAT(‘My’,’SQL’,’NULL’,’リファレンス’) |
+————————————————+
| MySQLNULLリファレンス |
+————————————————+
1 row in set (1.82 sec)

あ、こうかな。
mysql> SELECT CONCAT(‘My’,’SQL’,NULL,’リファレンス’);
+———————————————-+
| CONCAT(‘My’,’SQL’,NULL,’リファレンス’) |
+———————————————-+
| NULL |
+———————————————-+
1 row in set (1.91 sec)

なるほどー

Stack Overflowで質問しよう

Teratailで質問するより、stack overflowで世界に聞いた方が生産的なような気がしてきた。
ということで、stack overflowで質問してみます。

How to upgrade Laravel 5.2 → 5.8

When upgrading laravel from 5.2 to 5.8, do I need to upgrade one by one each with 5.3, 5.4, 5.5, 5,6, 5.7, 5.8, and check each details of changes?
Or can I go to 5.8 at once by composer json upgrade?
I would like to know general upgrade method of Laravel 5.2 → 5.8?

https://readouble.com/laravel/5.3/en/upgrade.html
https://readouble.com/laravel/5.4/en/upgrade.html
https://readouble.com/laravel/5.5/en/upgrade.html
https://readouble.com/laravel/5.6/en/upgrade.html
https://readouble.com/laravel/5.7/en/upgrade.html
https://readouble.com/laravel/5.8/en/upgrade.html

うん、こっちの方が英語のコミュニケーションの勉強にもなるし、よさそう。

早く気づけば良かった。。