dfatt 260 156,512
Красивый и практичный дизайн — моя страсть. Нравится хороший код, стремлюсь писать также красиво. Главное для меня в разработке и дизайне — это простота, и понятность.
Красивый и практичный дизайн — моя страсть. Нравится хороший код, стремлюсь писать также красиво. Главное для меня в разработке и дизайне — это простота, и понятность.
The code determines number of significant lines in the code. It skips newlines and comments. The plan is to skip doc-comments as well.
Can you improve it? Make it simpler and faster? Send your revisions. I think mine is the simplest one.
public function valid_length($code) {
$count = 0;
foreach (explode("\n", $code) as $line) {
$line = trim($line);
$line !== '' &&
$line !== '{' &&
$line !== '}' &&
preg_match_all('~(?://|[#])~', $line) !== 1 ? $count ++ : false;
}
if ($count > 18) {
$this->form_validation->set_message('valid_length', 'Your stash has more than 18 lines');
return false;
} else {
return true;
}
}
Most probably PHP 7 is going to have anonymous classes. Above is my use case for these. What are yours?
Would be glad to receive some ideas as revisions.
$image = new class {
public width = null;
public height = null;
public function __construct($filename) {
list($width, $height) = getimagesize($filename);
$this->width = $width;
$this->height = $height;
}
public function resize($width, $height) {
// some code
}
};
$image->resize(500, 500);
In C++, C# etc. you know what will be returned from a method. No surprises such as getting a string instead of expected int.
Using PHP 7 you can finally specify return type!
class Post extends CI_Model {
public function __construct() {
$this->load->database();
$this->load->helper(['url', 'date']);
}
/**
* Getting post by ID
* @param $id
* @return array
*/
public function get_post_by_id($id): array {
$query = $this->db->where('id', $id)->get('post');
return ! is_null($query->row()) ? $query->row() : [];
}
}
I've been searching for a good way to form a tree. I think this one is ideal. Can you make it better?
private function buildTree($data)
{
$tree = [];
$references = [];
foreach ($data as $id => &$node) {
$references[$node['id']] = &$node;
$node['children'] = [];
if (is_null($node['parent_id'])) {
$tree[] = &$node;
} else {
$references[$node['parent_id']]['children'][] = &$node;
}
}
return $tree;
}