编码规范

一致性非常重要,在编写开源代码时更是如此,因为代码属于数以百万的眼球,并且修复Bug依赖于这些数以万计的实际定位bug和理解如何解决它们.

这是为什么写Prestashop的任何代码: 主题(theme), 模块(module)或核心补丁(core patch), 都应该尽量遵循以下准则, 它是与Prestashop优雅集成的最可靠方式.

简化言之,代码一致性能保持代码的可读性和可维护性

主要约定

所有文件包含的代码都必须:

  • 只能使用UTF-8 无BOM 的编码格式.
  • 使用Unix LF (linefeed) line结尾.
  • 以一个单独的空白行结尾.

PHP代码约定

PHP文件必须遵循PSR-2 标准 以及Symfony 标准.

尽管建议Yoda conditions, they are not enforced.

可以配置PHP CS Fixer来帮助开发者在开发PrestaShop项目时遵守这些约定.

你可以执行下面命令:

php ./vendor/bin/php-cs-fixer fix

为了确保您永远不会忘记使您的代码符合要求,也可以使用git pre-commit提供的 sample

Javascript代码约定

Javascript文件必须遵循Airbnb Javascript style guide.

HTML, CSS (Sass), Twig 和 Smarty 代码约定

HTML, CSS (Sass), Twig 和Smarty文件必须遵循Mark Otto’s 编码标准. Mark是 Bootstrap 框架的创始人.

License 信息

所有的PrestaShop 文件都必须以PrestaShop license 块开始:

Core 文件

/**
 * 2007-2019 PrestaShop SA and Contributors
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * https://opensource.org/licenses/OSL-3.0
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 * versions in the future. If you wish to customize PrestaShop for your
 * needs please refer to http://www.prestashop.com for more information.
 *
 * @author    PrestaShop SA <contact@prestashop.com>
 * @copyright 2007-2019 PrestaShop SA and Contributors
 * @license   https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
 * International Registered Trademark & Property of PrestaShop SA
 */

Module 文件

/**
 * 2007-2019 PrestaShop SA and Contributors
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * https://opensource.org/licenses/AFL-3.0
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 * versions in the future. If you wish to customize PrestaShop for your
 * needs please refer to http://www.prestashop.com for more information.
 *
 * @author    PrestaShop SA <contact@prestashop.com>
 * @copyright 2007-2019 PrestaShop SA and Contributors
 * @license   https://opensource.org/licenses/AFL-3.0  Academic Free License (AFL 3.0)
 * International Registered Trademark & Property of PrestaShop SA
 */