Skip to content

Commit

Permalink
phpcs fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
tim1116 committed Mar 25, 2021
1 parent 798a5b6 commit 5430fb7
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 116 deletions.
56 changes: 0 additions & 56 deletions .php_cs

This file was deleted.

52 changes: 52 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

$header = <<<EOF
This file is part of the redisLock package.
Author:Tim Xiao
EOF;


return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules([
'@PSR2' => true,
'@Symfony' => true,
'array_syntax' => ['syntax' => 'short'],
'combine_consecutive_unsets' => true,
// one should use PHPUnit methods to set up expected exception instead of annotations
'general_phpdoc_annotation_remove' => ['expectedException', 'expectedExceptionMessage', 'expectedExceptionMessageRegExp'],
'header_comment' => array('header' => $header),
'heredoc_to_nowdoc' => true,
'no_extra_consecutive_blank_lines' => ['break', 'continue', 'extra', 'return', 'throw', 'use', 'parenthesis_brace_block', 'square_brace_block', 'curly_brace_block'],
'no_unreachable_default_argument_value' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'ordered_class_elements' => true,
'ordered_imports' => true,
'php_unit_strict' => true,
'phpdoc_add_missing_param_annotation' => true,
'no_trailing_comma_in_singleline_array' => true, //单行数组最后一个元素不添加逗号
'phpdoc_order' => true,
'psr4' => true,
'strict_comparison' => false,
'strict_param' => false, //这里设置为true,发现in_array方法会默认加上第3个参数为true,这使得in_array会对前两个参数值的类型也会做严格的校验,建议设置为false
'binary_operator_spaces' => ['default' => 'align_single_space_minimal'],
//'binary_operator_spaces' => true,
'concat_space' => ['spacing' => 'one'],
'no_empty_statement' => true,
'simplified_null_return' => true,
'no_extra_consecutive_blank_lines' => true,
'pre_increment' => false, //设置为false,$i++ 不会变成 ++$i
'native_function_invocation' => false, //in_array不会加前缀\
'cast_spaces' => ['space' => 'single'],
])
->setFinder(
PhpCsFixer\Finder::create()
->name('*.php')
->exclude('extend')
->exclude('vendor')
->exclude('FormBase')
->in(__DIR__)
)
->setUsingCache(false);
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
# redisLock

###
使用redis实现分布式锁

### Installation

### 关于代码格式
待补充


### 开发指南

###### 代码格式
使用PHP-CS-Fixer格式化代码
```
php-cs-fixer fix .
```
- 参考:https://github.com/FriendsOfPHP/PHP-CS-Fixer
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@
"psr-4": {
"tim1116\\redisLock\\": "src/"
}
},
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master"
}
}
37 changes: 20 additions & 17 deletions src/RedisLock.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
/**
* File: RedisLock.php
* PROJECT_NAME: redisLock

/*
* This file is part of the redisLock package.
*
* Author:Tim Xiao
*/

namespace tim1116\redisLock;
Expand All @@ -22,21 +24,13 @@ private function __construct(RedisSetting $setting)
$this->redisSetting = $setting;
}

private function checkKey(string $key):bool
{
if (empty($key)) {
return false;
}
return true;
}

public function lock(string $key, int $expire)
{
if (!$this->checkKey($key)) {
throw new \InvalidArgumentException("key error,unexpected empty");
throw new \InvalidArgumentException('key error,unexpected empty');
}
if ($expire <= 0) {
throw new \InvalidArgumentException("expire time error");
throw new \InvalidArgumentException('expire time error');
}
$redis = $this->connect();
$key = Util::lockKey($key);
Expand All @@ -45,25 +39,27 @@ public function lock(string $key, int $expire)
if (!$isLocked) {
return false;
}

return true;
}

// unlock
public function unLock(string $key)
{
if (!$this->checkKey($key)) {
throw new \InvalidArgumentException("key error,unexpected empty");
throw new \InvalidArgumentException('key error,unexpected empty');
}
$redis = $this->connect();

return $redis->del(Util::lockKey($key));
}

/**
* 连接redis
* 连接redis.
*
* @throws \RedisException
*/
public function connect():\Redis
public function connect(): \Redis
{
$redis = new \Redis();
$redis->connect($this->redisSetting->host, $this->redisSetting->port, $this->redisSetting->timeout);
Expand All @@ -73,10 +69,17 @@ public function connect():\Redis
$redis->select($this->redisSetting->dbindex);
if ($this->redisSetting->prefix) {
$redis->setOption(\Redis::OPT_PREFIX, $this->redisSetting->prefix);

}

return $redis;
}

private function checkKey(string $key): bool
{
if (empty($key)) {
return false;
}

return true;
}
}
20 changes: 10 additions & 10 deletions src/RedisSetting.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
<?php
/**
* File: RedisSeeting.php
* PROJECT_NAME: redisLock

/*
* This file is part of the redisLock package.
*
* Author:Tim Xiao
*/

namespace tim1116\redisLock;

/**
* redis配置类
* Class RedisSetting
* Class RedisSetting.
*/
class RedisSetting
{
public $host = '127.0.0.1';
public $port = 6379;
// 连接超时时间 默认3S
public $timeout = 3.0;
public $timeout = 3.0;
public $password = '';
public $dbindex = 0;
public $dbindex = 0;
// 前缀
public $prefix = 'redisLock:';

public function __construct(Array $config = [])
public function __construct(array $config = [])
{
$this->setConfig($config);
}


public function setConfig(Array $config)
public function setConfig(array $config)
{
if (isset($config['host'])) {
$this->host = $config['host'];
Expand All @@ -52,6 +53,5 @@ public function setConfig(Array $config)
if (isset($config['prefix'])) {
$this->prefix = $config['prefix'];
}

}
}
7 changes: 6 additions & 1 deletion src/Util.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<?php


declare(strict_types=1);

/*
* This file is part of the redisLock package.
*
* Author:Tim Xiao
*/

namespace tim1116\redisLock;

class Util
Expand Down
43 changes: 22 additions & 21 deletions src/traits/Single.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
<?php
/**
* File: Single.php
* PROJECT_NAME: redisLock

/*
* This file is part of the redisLock package.
*
* Author:Tim Xiao
*/

namespace tim1116\redisLock\traits;

trait Single
trait Single
{
private static $instance = null;

/**
* prevent the instance from being cloned (which would create a second instance of it).
*/
private function __clone()
{
}

/**
* 防止通过反序列化生成类实例.
*
* $obj1 = Singleton::getInstance();
* $obj2= unserialize(serialize($obj1));
*/
private function __wakeup()
{
}

/**
* 初始化.
Expand All @@ -26,21 +44,4 @@ public static function getInstance(...$args)

return static::$instance;
}

/**
* prevent the instance from being cloned (which would create a second instance of it)
*/
private function __clone()
{
}

/**
* 防止通过反序列化生成类实例
*
* $obj1 = Singleton::getInstance();
* $obj2= unserialize(serialize($obj1));
*/
private function __wakeup()
{
}
}
15 changes: 6 additions & 9 deletions tests/test1.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
<?php
/**
* File: test1.php
* PROJECT_NAME: redisLock

/*
* This file is part of the redisLock package.
*
* Author:Tim Xiao
*/

require __DIR__ . '/../vendor/autoload.php';

use tim1116\redisLock\RedisLock;
use tim1116\redisLock\RedisSetting;


$config = [
'host' => '192.168.199.101',
'port' => 6379,
'password' => '123456',
'dbindex' => 6,
];
$obj = RedisLock::getInstance(new RedisSetting($config));
//$isLock = $obj->lock("aaa",100);
//var_dump($isLock);

echo "unlock";
var_dump($obj->unLock("aaa"));
$isLock = $obj->lock('aaa', 100);
var_dump($isLock);
Loading

0 comments on commit 5430fb7

Please sign in to comment.