shipfi

add php rules

......@@ -50,9 +50,7 @@ windows的cms工具,可以使用ConEmu或者cmder工具
开发工具下载:
phpstorm : [下载链接](https://www.jetbrains.com/phpstorm/download/#section=windows)
? 注:phpstorm64位版本依赖于JRE
phpstorm : [下载链接](https://www.jetbrains.com/phpstorm/download/#section=windows) 注:phpstorm64位版本依赖于JRE
phpstorm的注册激活 : [参考](http://idea.lanyus.com/)
......@@ -66,6 +64,8 @@ Conemu下载 [下载链接](http://conemu.github.io/en/Downloads.html)
### 5. 关于PHP默认编码规范
* [5.1 PHP语言编码规范 v1.0](php_rule.md)
......@@ -73,21 +73,27 @@ Conemu下载 [下载链接](http://conemu.github.io/en/Downloads.html)
### 6. 数据库规范
* 6.1 [数据库规范](database.md)
### 7. GIT代码管理规范
* 7.1 [GIT Flow规范](git_flow.md)
### 8. 项目开发流程规范
* 8.1 [产品设计规范]()
* 8.1 [文档规范]()
* 8.2 [项目开发规范]()
* 8.3 [测试规范]()
* 8.4 [生产部署规范]()
......
### PHP自定义类示例(Weixin消息解析类)
```php
/**
* Created by Qingger.
* User: jsspf
* Date: 2017/3/24
* Time: 10:50
*/
namespace App\Service;
use App\Exception\TPException;
use App\Library\VarDefines\GlobalErrCode;
/**
* 微信公众号消息的接收与回复
* @desc : 消息加解密指引参考 https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419318611&lang=zh_CN
* Class WXMPEventMessageParser
* @package App\Services\WeixinFunc
*/
class WeixinMPEventMessageParser extends WeixinBaseFuncService
{
/**
* @var object
* 接收消息的消息体结构 (text | image | voice | video | shortvideo | location | link)
*/
private $_messageReceiveBody = null;
/**
* @var string
* 接收消息的发送方帐号
*/
private $_messageReceiveFrom = null;
/**
* @var string
* 接收消息的接收方帐号
*/
private $_messageReceiveTo = null;
/**
* @var string
* 接收消息的消息体类型 (text | image | voice | video | shortvideo | location | link)
*/
private $_messageReceiveType = null;
/**
* @var string
* 接收消息时,Request请求中的nonce信息
*/
private $_messageNonce = '';
/**
* @var WXBizMsgCrypt
* 微信消息体加解密对象
*/
private $_wxBizMsgCrypt = null;
/**
* @var array
* 公众号消息回复的模板
*/
private $_replyMessageTemplate = [
// 文本消息的回复模板
'text' => "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>",
// 图片消息的回复模板
'image' => "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[image]]></MsgType>
<Image><MediaId><![CDATA[%s]]></MediaId></Image>
</xml>",
];
public function __construct($tpSuiteName,$messageNonce='')
{
parent::__construct();
$this->_messageNonce = $messageNonce;
$this->_wxBizMsgCrypt = new WXBizMsgCrypt(
$this->getWxTpConfigParser()->getConfigToken($tpSuiteName),
$this->getWxTpConfigParser()->getConfigEncodingAesKey($tpSuiteName),
$this->getWxTpConfigParser()->getConfigId($tpSuiteName)
);
}
/**
* 对消息体进行微信的加密
* @param $replyMsgStr
* @return string
*/
private function _encryptMessage($replyMsgStr) {
$replyMessageEncrypt = '';
$this->_wxBizMsgCrypt->encryptMsg($replyMsgStr,time(),$this->_messageNonce,$replyMessageEncrypt);
return $replyMessageEncrypt;
}
/**
* 生成文本消息的回复结构
* @param $content
* @return string
*/
public function textMessageReply($content) {
$replyMessageType = 'text';
$replyMsgStr = sprintf($this->_replyMessageTemplate[$replyMessageType],
$this->_messageReceiveFrom,
$this->_messageReceiveTo,
time(),
$content);
return $this->_encryptMessage($replyMsgStr);
}
/**
* 生成图片消息的回复
* @param $imageMediaId
* @return string
*/
public function imageMessageReply($imageMediaId) {
$replyMessageType = 'image';
$replyMsgStr = sprintf($this->_replyMessageTemplate[$replyMessageType],
$this->_messageReceiveFrom,
$this->_messageReceiveTo,
time(),
$imageMediaId);
return $this->_encryptMessage($replyMsgStr);
}
/**
* Todo : 需要实现,使用XML对象操作字符串模板
* 生成音乐消息的回复
* @param array $musicInfo
* [
* 'title' : 标题 / 可选
* 'description' : 描述 / 可选
* 'musicURL' : 音乐链接 / 可选
* 'hqMusicUrl' : 高质量音乐链接,WIFI环境优先使用该链接播放音乐 / 可选
* 'ThumbMediaId' : 缩略图的媒体id,通过素材管理接口上传多媒体文件,得到的id / 可选
* ]
* @throws TPException
* @return string
*/
public function musicMessageReply(array $musicInfo) {
throw new TPException('Music Message Reply Not Support',GlobalErrCode::ERR_FUNCTION_NOT_IMPLEMENT);
}
/**
* 解析Weixin Message消息,并根据相应的消息,进行回复
* @desc 具体解密参考:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419318479&lang=zh_CN
* @param $messageEventObj
* @param Closure $doEvent
* @return mixed
* @throws TPException
*/
public function parseEvent($messageEventObj,Closure $doEvent) {
if(isset($messageEventObj->MsgType)) {
$this->_messageReceiveBody = $messageEventObj;
$this->_messageReceiveFrom = isset($messageEventObj->FromUserName)?$messageEventObj->FromUserName:null;
$this->_messageReceiveTo = isset($messageEventObj->ToUserName)?$messageEventObj->ToUserName:null;
$this->_messageReceiveType = $messageEventObj->MsgType;
$messageReply = $doEvent($messageEventObj);
return $messageReply;
} else {
throw new TPException('MP Message Body Parse Error',GlobalErrCode::ERR_WEIXIN_MESSAGE_INFO_ERROR);
}
}
```
## PHP语言规范
本规则适用于使用PHP语言编写应用的人员。一经定义,PHP编写时统一遵循此规范
本规范包括PHP编写时命名、缩进、文件结构、注释等一系列定义。
**目标 :公司PHP编码形成统一风格,编码人员能够互相理解对方编写之意图。**
### 1. 代码规范
##### 1.1 文件夹命名
* 如果有框架定义,则文件夹命名统一按照框架制定的规则,如果框架不定义,则统一使用小写字母。
> 以Lumen为例,项目下的第一级目录全部以小写字母命名。除app文件夹外,其它目录也是全部使用小写字母命名。 app目录下文件夹统一使用首字符大写命名。
---
##### 1.2 文件及命名
* 类文件,命名与类名称保持一致,**必须**统一使用**大驼峰**形式。
```php
// CacheManger.php
class CacheManger {
//...
}
```
* 配置文件,**必须**统一使用字母小写形式命名
```php
// config/jwt.php
return [
//...
]
```
* 普通工具脚本,**必须**统一使用小驼峰命名
```php
// tools/uploadImage.php
$i=1;
dosth();
```
* 只含有php代码的文件,结尾处忽略掉 "?>", 以防止多余的空格或者其它字符影响到代码。
* 文件的编码**必须**为UTF-8字符集
* 文件换行**必须**以Unix换行\n为准,**不允许**出现\r\n换行。 注:通过git可配置
* 如果是接口类,则文件以I开头,如 IAuthedUser。 如果是抽象类,则以Abs开头,如AbsAppClient
* 接口类和抽象类的实现,必须在文件命名中强调其继承哪个接口或抽象.
```php
// 命名的准确
interface IAuthedUser {}
abstract AbsAppClient {}
class WeixinAuthedUser implements IAutheduser { }
class MessageAppClient extends AbsAppClient {}
```
---
##### 1.3 变量命名
* 全局变量命名使用大驼峰,前缀加上_,所有单词首字母大写。
* 常量统一使用大写,中间分隔使用_
* 私有变量命名小驼峰,前缀加上_
* 变量命名确认以英文名词为主。
* 变量如果是类的对象,**必须**使用注释,使用@var定义对象所属的类
* **必须**注意单复数,变量如果是数组列表,则以s/es/List为结尾
* 变量定义示例
```php
// 全局变量
$_System_Config = config('myconfig');
$_Root_Path = '/';
// 常量
define('CURRENT_SCRIPT','index_php');
const TRANSCATION_TYPE = 'income';
// 私有变量(protected不在此列)
private $_orderCnt = 0;
protected $_salaryAmount = 0;
/**
* 我的订单列表
* @var array
*/
private $myOrderList = [];
/**
* 客户连接对象
* @var ClientConnection|null
*/
private $peerConnection = null;
```
---
##### 1.4 类、方法、对象命名
* 类统一使用大驼峰形式命名,与文件名必须保持一致。
* 自编写的类一般遵循名词或名词短语来进行命名。
* 框架中的一些类的命名参考 ()
* 类的动作方法 ,一般使用【动词+名词】方式进行命名,例如 sendMessage / getAttr / setAttr / postLogin等
* 类的属性和变量字段,使用小写字母或者小写驼峰方式命名
* 类的private字段和方法,**必须**使用带_前缀的方式命名
* 常量值,统一使用大写。
* [PHP自定义类示例参考](Samples/my_class_sample.md)
---
##### 1.5 缩进和注释
* 缩进采用4空格进行缩进,禁止使用TAB制表符。
* 所有的类、函数、方法定义都需要进行注释。
* 一般php注释遵循phpdocumentor规范, [phpdocumentor](https://phpdoc.org/docs/latest/references/phpdoc/index.html)
* 如果是API的接口,则提供PHP APIDOC的相关注释 [APIDOC](https://github.com/calinrada/php-apidoc)
* 注释
```php
// ------- 文件的注释 -------
/**
* Created by Qingger.
* User: shipfi
* Date: 2016/9/22
* Time: 15:49
*/
// ------- 类的注释 -------
/**
* 所有应用Controller的基类,提供了一般性请求/应答的共通实现
* @desc 如有必要,通过desc描述详细的类的功能
* Class BasicController
* @package App\Http\Controllers
*/
class BasicController extends Controller
{
// ------- 变量的注释,如果变量为 Array/Object类型,一定要使用@var说明其类型定义 -------
/**
* 缓存管理对象
* @var AppCacheManager|null
*/
protected $appCacheManager = null;
const MY_RET = GlobalErrCode::ERR_SUCCESS;
/**
* @param Request $request
* @param AppCacheManager $appCacheManager
*/
public function __construct(Request $request,AppCacheManager $appCacheManager){
$this->requestHandler = $request;
$this->appCacheManager = $appCacheManager;
DB::connection()->enableQueryLog();
}
// ------- 方法的注释,参数如果为Array/Object类型,则一定需要指明 -------
/**
* 记录Debug日志信息
* @desc : 参考----
* @param $message
* @param array $moreInfo
*/
protected function debugLogIt($message,array $moreInfo=[]) {
Log::debug($message,$moreInfo);
}
}
```
---
##### 1.6 代码和语句
* 两元运算符,前后使用空格
```php
$ret = $age>18 ? 'adult' : 'children';
```
* 变量赋值必须保持相等间距和排列
```php
$newQyAgents = array_values(array_rename_keys($qyAgents,[
'agent_id' => 'agentId',
'agent_name' => 'agentName',
'agent_round_logo_url' => 'agentRoundLogo',
'agent_square_logo_url' => 'agentSquareLogo',
'agent_app_id' => 'appId'
]));
if($corpMangerIns) {
$ret['corpUserManger']['corpUserId'] = $corpMangerIns->getMyUserId();
$ret['corpUserManger']['corpUserEmail'] = $corpMangerIns->getMyUserEmail();
$ret['corpUserManger']['corpUserMobile'] = $corpMangerIns->getMyUserMobile();
}
```
* 每行代码长度应控制在80个字符以内,最长不超过120个字符, 如超过则另起一行
```php
$qyAuthAppGrpIns = (new QyAuthAppGrp())->getInstanceBySuiteNameCorpId(
$suiteName,
$this->requestHandler->input('corpId')
);
```
* 每行结尾不允许有多余空格
* 在类中(尤其是在Model),对于每个属性的访问, 统一使用get/set方法
```php
class MPAuthAppGrp extends BaseModel
{
public function getMyCallBackUrl() { return isset($this->grp_callback_url) ? $this->grp_callback_url : null; }
public function getMyGroupAppId() { return isset($this->grp_app_id) ? $this->grp_app_id : null; }
public function getMyWXAppId() { return isset($this->auth_wx_app_id) ? $this->auth_wx_app_id : null; }
public function getMyWXAppName() { return isset($this->auth_wx_app_name) ? $this->auth_wx_app_name : null; }
public function getMyGrpCode() { return isset($this->grp_code) ? $this->grp_code : null; }
public function getMyOpenAppId() { return isset($this->opentp_appid) ? $this->opentp_appid : null; }
public function getMyOpenAppName() { return isset($this->opentp_name) ? $this->opentp_name : null; }
}
```
* 语句流程
```php
/** if / else 语句规范 **/
if($age>1 && $age<10) {
echo '1';
} else if($age>30 && $age<=60) {
echo '2';
} else {
echo '3';
}
/* switch语句规范 */
switch($status) {
// 严禁使用魔数,如 case 10:
case OrderStatus::NO_PAY_STATUS:
echo '1';
break;
case OrderStatus::HAS_PAY_STATUS:
echo '2';
break;
default:
break;
}
/* while语句 */
while($condition) {
// do sth
}
/* foreach语句 */
foreach($a)
```
* 使用 array 类型符声明关联数组的时候,我们鼓励把它分成多个行,只是我们必须同时保证每行的键与值的对齐,以保持美观。
* 类中的所有代码都必须用四个空格来进行缩进。
* 每个 php 文件只允许声明一个类。
* 任何类变量的声明都**必须**放在类顶部,先于任何函数的声明。
* 类中的方法必须总是用 private,protected 或者 public 来声明其作用域。
* 对于方法的变量参数,如果参数确定为对象或数据,则**必须**在参数中给出变量提示:
```php
public function foo(AbsContract $obj, array options) {
// do sth
}
```
* 代码做到复用,对于相同听代码逻辑,严禁出现在两个函数或者两个文件内,如果逻辑相同,使用重构提取共用性。
* 一个函数的逻辑实现**严禁**超过一屏(非常特殊或者逻辑简单可理解的除外)。如果业务复杂,使用多个函数实现。
* 一个函数中**不能**出现以下复杂的if/else逻辑判断, 简单规则,每个函数只允许一个if/elseif/else嵌套层。
```php
if($condition1) {
if($condition2) {
// do sth
} else if ($condition3) {
// do sth
} else {
if($condiont4) { // do sth.. }
// do sth
}
} else {
// do sth
}
```
---
##### 1.7 变量的定义和使用
* 代码中,类的变量必须做到先定义后使用.
* 变量使用时,尤其是数组变量,一定要判断其是否被设置
```php
$sItem = isset($items['key']) ? $item['key'] : null;
```
* 在定义和使用变量时一定要明确其类型
```php
// 模型变量定义
$objectModel = new ObjectModel();
$objectInstance = $objectModel->find(1);
// 基本类型:boolean
$isAuthed = $objectInstance->getMyAuthedFlag();
// 基本类型:int
$cntAccount = 1;
// date类型
$dtToday = Carbon::now();
$tsExpires = time() + 3800;
// 基本类型:array
$accountList = [1,2,3];
$acounts = [1,2,3];
// 基本类型对象: object
$clientInstance = $this->getClient();
// foreach使用时尤其是需要明确变量的单复数
foreach($items as $key=>$item) {
// do sth
}
```
?
\ No newline at end of file