exakat.1.5.3 review

exakat.1.5.3 Review

exakat.1.5.4 add more after-coding review for your code : after suggesting upgrading visibility on properties, it now checks class candidates for final and abstract keyword. As you can guess, a class won’t be a candidate for them at the same time. We also include more review of generators, including the PHP classic trap with 迭代器_to_array(), and code improvement with yield from. I love it when the Exakat 1.5.3 review comes together.

使用产量

屈服 from was introduced in PHP 7. It is a delegation keyword for yield : the yielding will be handed to another function or method, and when this delegated yield has been finished, the next will resume.

This means that yield from is basically a foreach loop for a designated generator, and may be used just like that :

  
<?php

function bar_good() {
    yield 1;
   yield from foo();
    yield 5;

}

function bar_bad() {
    yield 1;
    foreach(foo() as $f) {
        yield $f;
   }
    yield 5;

}
// For context
function foo() {
    yield 2;
    yield 3;
    yield 4;
}

?>

Note that yield from works with any Traversable object and arrays, on top of of other Generator. This means that the following are basically the same :

  
<?php

function bar_bad() {
   foreach(array('a', 'b', 'd', 'e', 'f') as $i) {
      yield $i;
   }
}

function bar_good() {
    yield from array('a', 'b', 'd', 'e', 'f');
}
?>

它可能不值得将array()上的foreach()转换为金沙上线发电机,但如果代码与多个数据源杂耍,则构建这样的发电机非常方便。

Using yield from not only hides the foreach loop, but it is also faster to execute.

谨防迭代器大批()

The yield keyword was introduced in PHP 5.5, to build generators. Those are used in foreach loops, and produce one value each time. To build an array from a generator, there is the convenient function 迭代器_to_array(), which collects all the values into one array, saving the call of a foreach().

  
<?php
function from() {
    yield 1; // key 0
    yield 2; // key 1
    yield 3; // key 2
}
function gen() {
    yield 0; // key 0
    yield from from(); // keys 0-2
    yield 4; // key 1
}
// returns [1, 4, 3]
var_dump(iterator_to_array(gen()));
?>

作为 PHP手册 says, the yield keyword provides automatic keys but doesn’t reset those when calling yield from. As such, mixing yield and yield from in the same generator is bound to lead to overwrite values in the final array.

解决此问题的解决方案包括:使用 大批_merge. 使用后合并数组 迭代器大批;屈服时使用键。

  
<?php
function from() {
    yield 1 => 1; // key 1
    yield 2 => 2; // key 2
    yield 3 => 3; // key 3
}
function gen() {
    yield 0; // key 0, automatic
    yield from from(); // keys 1-3
    yield 4 => 4; // key 4
}
// returns [0 1, 2, 3, 4]
var_dump(iterator_to_array(gen()));
?>

在此示例中,我们只需将值重用为键,并最终以不同的键。如果真实代码,生成器被任意组合时,请在调用迭代器后合并数组大批()是金沙上线更好的主意。

谢谢 Holger Woltersdorf. 跑进

代码后的最终或抽象

final and abstract are two keywords that apply to classes and methods. They signal classes that can’t or must be extended, respectively. A final class can’t be found as extends, and abstract classes must be extends.

当代码的架构师对其进行广泛的视野时,它们最常被设置为概念时间,并且在代码中添加了限制的自由度。当代码构想轻微强制执行时,或者如果架构有机地生长,则代码将在没有这些关键字的情况下写入。

How can we check if those keywords are applicable ? Since the code is already written, we simply have to reverse the logic of the rule : a final class is a class that is never extended, but instantiated; an abstract class is a class that is extended, but never instantiated directly.

  
<?php

// Instantiated but never extended class
class couldBeFinal {      }
new couldBeFinal(); 

// Extended but never instantiated class
class couldBeAbstract {      }
class couldNotBeAbstract extends couldBeAbstract {      }

?>

请使用这个例子,温柔:它很小,用于编辑目的。另一方面,审查2000类强大的应用程序来检查哪个延伸,实例化的是金沙上线令人生畏的任务:因此,它通常延迟,直到从未完成。

所有这些实际上都是静态分析的好任务。 exakat通过整个存储库中收集所有这些操作,并在大使报告中报告这种情况(默认报告)。在这里,你’ll find every class that may be upgraded to final or abstract.

The red stars mention possible upgrades by using final or abstract keywords; The green stars mention a valid absence of the option (an extended class, that can’例如,T是最终的;缺乏明星报告目前已配置的课程:好工作。

Is it enough to decide if a class may be final or abstract : probably not. This report is a great base to review the current class configuration, and ponder about the adding of such options. It is also interesting to review the list, and find classes that ought to be listed, but didn’t make it : maybe those abstractClass that were supposed to be extended did find some other usage. In any case, time for a code review!

每周审计:2018,周#46

exakat包括A.‘weekly’报告:此报告采用五项分析为构建。这意味着短暂的审计报告,几乎没有问题审查。读取它们并不是很多,并在代码中查看它们。 PHP社区中的每个人都可以专注于金沙上线经典的编码问题并解决它。谈谈周围的每周审计:你’LL找到面临同样挑战的程序员。

获得‘weekly’审核,运行审计,请求‘Weekly’ report.

# Init the project (skip when it is already done)  php exakat.phar init -p <yourproject> -R //github.com/Seldaek/monolog.git -git 

# Run the project (skip when it is already done)  php exakat.phar project -p <yourproject> 

# Export the weekly project (every monday)  php exakat.phar report -p <yourproject> -format Weekly 

# Open projects/<yourproject>/weekly/index.html in your browser   

每周,您可以在此找到5个新分析,以便在您的代码中审查。事实上,当您的代码干净时,您也可以快速查看即将到来的

每周 recommendations for PHP code review : 2018, week 2018-46

每周,您可以在此找到5个新分析,以便在您的代码中审查。事实上,当您的代码干净时,您也可以快速查看即将到来的一周‘Go further’ section.

快乐的PHP代码评论

所有362个分析仪都介绍在文档中,包括忠诚 切换而不默认 :始终在switch()中使用默认语句..它是金沙上线例程错误:56%应用程序Mishandle Switch()。

您可以查看图库中的所有exakat报告: exakat画廊.

下载exakat.io,安装它 Docker.,升级它‘exakat.phar升级-u’ and like us on GitHub..