"Ah ouais, genre les conditions ternaires ?"
- Quelqu'un, la semaine dernière
// om_modules/accounting/observers/AccountingRefundRequestObserver.php
public function update(SplSubject $oSubject)
{
// ...
try {
if ("RefundRequest" == get_class($oSubject)) {
// ...
if ($oPayment) {
if ('DC' == $oPayment->getPlugin() && $oSubject->getAmountRefunded() > 0) {
// ...
foreach ($aoOrderLine as $oOrderLine) {
// ...
if ($oEcatProduct) {
// ...
if ($oEcatNomenclature) {
if (Application::ESMILE_BT == $oEcatProduct->getIdApp()) {
$oAccountingLoyalty->setIdSector("B".$oEcatNomenclature->getIdElement());
} else {
$oAccountingLoyalty->setIdSector($oEcatNomenclature->getIdElement());
}
} else {
$oAccountingLogger->error(/* ... */);
}
// ...
}
}
}
}
}
} catch (Exception $e){
$oAccountingLogger->error(/* ... */);
}
}
Éviter l'imbrication de structures logiques
Sortir dès que possible (return, throw, break, continue)
public function update(SplSubject $oSubject)
{
// ...
try {
if ("RefundRequest" != get_class($oSubject)) {
return;
}
// ...
if (null === $oPayment || 'DC' != $oPayment->getPlugin() || 0 >= $oSubject->getAmountRefunded()) {
return;
}
// ...
foreach ($aoOrderLine as $oOrderLine) {
// ...
if (null === $oEcatProduct) {
continue;
}
// ...
if (null === $oEcatNomenclature) {
throw new Exception(/* ... */);
}
$idSector = $oEcatNomenclature->getIdElement();
if (Application::ESMILE_BT == $oEcatProduct->getIdApp()) {
$idSector = 'B'.$idSector;
}
$oAccountingLoyalty->setIdSector($idSector);
// ...
}
} catch (Exception $e){
$oAccountingLogger->error(/* ... */);
}
}
public function createRefundAction($aiIdOrderItem, $iIdReason)
{
return PaymentRefundServices::createRefund($aiIdOrderItem, $iIdReason);
}
Single responsibility principle
// busmodules/product/plugin/article/views/AjaxController.php
private function _saveUnitAssoc($iIdUnitCurrent, $sCurrentPrefix, $aArticlesInfos)
{
// ...
if ($aArticlesInfos == null) {
$aArticlesInfos = array();
}
$tmp = is_array($aArticlesInfos) ? $aArticlesInfos : array($aArticlesInfos);
// ...
}
private function _saveUnitAssoc($iIdUnitCurrent, $sCurrentPrefix, array $aArticlesInfos)
{
// Si on passe autre chose qu'un tableau, on se mange une fatal, et c'est bien fait
}
"Et si je veux rendre le paramètre facultatif ?"
- Toi là-bas (je t'ai entendu)
private function _saveUnitAssoc($iIdUnit, $sCurrentPrefix, array $aArticlesInfos = null)
{
// Pas mal
}
private function _saveUnitAssoc($iIdUnit, $sCurrentPrefix, array $aArticlesInfos = array())
{
// Mieux
}
Dependency inversion principle
// busmodules/order/OrderModule.php
public static function createLfcodebarreEntryByIdFacture($iIdFacture, $iIdSupplyingOrder)
{
$oOrderModule = OrderModule::getInstance("order");
$aoLfProduitLines = new LfproduitIterator('idfacture = '.$iIdFacture);
//...
$aProductShippingFeeBarcode = $oOrderModule->getProductsShippingBarcode();
// ...
}
public function createLfcodebarreEntryByIdFacture(
OrderModule $module,
LfproduitIterator $iterator,
$iIdSupplyingOrder
) {
$aProductShippingFeeBarcode = $module->getProductsShippingBarcode();
// ...
}
class MasterDataProductTransformer implements TransformerInterface
{
public function transform(Produit $product)
{
$masterDataProduct = new MasterDataProduct();
$masterDataProduct->setId($product->getIdproduit());
$masterDataProduct->setName($product->getNomproduit());
return $masterDataProduct;
}
}
Comment gérer la transformation des produits contenus dans un itérateur ?
class ProduitTransformableIterator extends ProduitIterator
{
public function transformAll(TransformerInterface $transformer)
{
$products = array();
foreach ($this as $product) {
$products[] = $transformer->transform($product);
}
return $products;
}
}
class ProduitSomethingSomethingIterator extends ProduitTransformableIterator
{
// ...
}
class IteratorTransformerService
{
private $transformer;
public function __construct(TransformerInterface $transformer)
{
$this->transformer = $transformer;
}
public function transformAll(Iterator $iterator)
{
$results = array();
foreach ($iterator as $item) {
$results[] = $transformer->transform($item);
}
return $results;
}
}
trait TransformableIterator
{
public function transformAll(TransformerInterface $transformer)
{
$results = array();
foreach ($this as $item) {
$results[] = $transformer->transform($item);
}
return $results;
}
}
class ProductSomethingSomethingIterator
{
use TransformableIterator;
}
"Ideally, all of the code should look like it was written by one person in one sitting."
- Quelqu'un sur Stack Overflow qui a bien raison
Des questions ?