php7.0とphp7.1の違い

1. 型宣言

function type_defined(int $argument) : string {
	return 'Hello';
}

あまり見ない気がしますが。。

$name_list = [
	'luna',
	'sunny',
	'star',
];

function type_defined(int $name_id) : string {
	return isset($name_list[$name_id]) ? $name_list[$name_id] : null; 
}

var_dump(get_name(4));

barryvdh/laravel-debugbar

debuggerはlaravel開発に役立つらしい

ただし、パスワードも丸見えになるらしい。
[vagrant@localhost tea]$ php composer.phar require barryvdh/laravel-debugbar
Using version ^3.2 for barryvdh/laravel-debugbar
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 2 installs, 0 updates, 0 removals
– Installing maximebf/debugbar (v1.15.0): Downloading (100%) – Installing barryvdh/laravel-debugbar (v3.2.4): Downloading (100%)
maximebf/debugbar suggests installing kriswallsmith/assetic (The best way to manage assets)
maximebf/debugbar suggests installing predis/predis (Redis storage)
Writing lock file
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover –ansi
Discovered Package: barryvdh/laravel-debugbar
Discovered Package: barryvdh/laravel-dompdf
Discovered Package: beyondcode/laravel-dump-server
Discovered Package: fideloper/proxy
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Package manifest generated successfully.

league/flysystem-aws-s3-v3

LaravelのファイルストレージとS3を連携させるには、専用のパッケージを導入する必要があるとのこと。

composer.json

"require": {
        "php": "^7.1.3",
        "barryvdh/laravel-dompdf": "^0.8.4",
        "fideloper/proxy": "^4.0",
        "laravel/framework": "5.8.*",
        "laravel/tinker": "^1.0"
    },

ここに、league/flysystem-aws-s3-v3を追加する。

laravel/config/filesystem

's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
        ],

env(${})となっているので、.envファイルで編集します。

AWS_KEY=[アクセスキー]
AWS_SECRET=[シークレットキー]
AWS_REGION=[リージョン(東京ならap-northeast-1)]
AWS_BUCKET=[バケット名]

そういうことかー

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)

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