Friday, May 17, 2019

PHP code quality and static code analysis tools


There are multiple tools exists to improve PHP code quality and static code analysis. Due to varied in nature, configure these tools in PHP project is many a time troublesome.

Solution is Docker. Docker image to configure PHP code quality and static code analysis. Being a PHP developer, some of us might not familiar with Docker.

As per Wikipedia, the Docker is a collection of interoperating software-as-a-service and platform-as-a-service offering that employ operating-system-level virtualization to cultivate development and delivery of software inside standardized software packages called container.

Being a developer, you do not need to worry about how Docker work in our operating system. The most important thing is to know how we can increase productivity and goodies using Docker.

Let me give some glance of Docker and its applicability in PHP web development.

I have released PHP code quality and static code analysis project

This project you can use to analyze your code. It is single source of truth for various code quality related tools.

To use this project you need to install Docker in your OS.

Brief about Code quality and static analysis tools

PhpMetrics: PhpMetrics provides various metrics about PHP projects. You should visit demo project to get an understanding about this tool capability.

PHP Code Sniffer: PHP Code Sniffer is a set of two PHP scripts; the main ```phpcs``` script that tokenizes PHP, JavaScript and CSS files to detect violations of a defined coding standard, and a second  ```phpcbf``` script to automatically correct coding standard violations. The PHP CodeSniffer is an essential development tool that ensures your code remains clean and consistent. Refer

PHPStan: PHP Static Analysis Tool - discover bugs in your code without running it. To get more details about this tool, refer  https://github.com/phpstan/phpstan


Monday, April 30, 2018

Magento 2 console memory size issue

Use below command when you want to execute some Magento 2 console command and face "Fatal error: Allowed memory size of ..." kind of issue

php -dmemory_limit=1G bin/magento <your command>

Where memory_limit is set for 1 G it can be change as per your type of command and operation what you are executing using Magento 2 console.

Sunday, April 8, 2018

Install Drupal 8 using git and composer


To install Drupal 8.5.x

 Using git

git clone --branch 8.5.x https://git.drupal.org/project/drupal.git

User composer

composer create-project drupal/drupal <<your site name>>

Note: "<<your site name>>" to be replace with your name of the site. This command will download latest version of Drupal 8.

Tuesday, February 13, 2018

How to debug query in Drupal 8

This is something what every developer want to know, how and what is going on when Drupal provide any result from query.

"How this query construct"

Well, it is not difficult, I will share some detail which gives you information about query construct and its parameters.

To debug any query, Drupal 8 provides addTag option. Before going in details first we should know about Tag and its usage.

Tags are simple string which identify query. In one query, there will be more than one tags. The main use is to alter hooks and take specific action based on specified tag. Tags should be all lower-case and it contains only letters numbers and underscore, and start with a letter. All rules of identifies applied to the Tag.

Coming to point, to debug any query  in Drupal add debug in tags as below,

$query->addTag('debug');

Where query is the query object, this line can be applicable with entity query, View query alter, search query alter and many other places.

If you like this post, please provide your comments.

Saturday, September 16, 2017

Drush was not able to start (bootstrap) the Drupal database.

I have struggled lot to resolve "Drush was not able to start (bootstrap) the Drupal database..." issue in Drupal 8. Finally, after burning the midnight oil I have found solution as below.

My laptop configuration: PHP 7, WAMP 3, Windows  7 and 10

The main reason of such issue is PATH variable. You need to add your MySQL and PHP path in system PATH variable. To check, you should close your command shell and restart. It will start using updated PATH variable


In my system, I am having WAMP  3. Path for PHP and MySQL path as below

Path=D:\wamp64\bin\mysql\mysql5.7.14\bin\;D:\wamp64\bin\php\php7.0.10\;

 Credit goes to https://www.drupal.org/node/1393232

Sunday, September 3, 2017

Resetting Admin password in Drupal 8


Option 1

drush sqlq "select name from users where uid=1"
drush upwd admin --password=pass

 

Option 2

Generate Password hash

php core/scripts/password-hash.sh 'your-new-pass-here'

Update in database

UPDATE users_field_data SET pass='$S$E5j59pCS9kjQ8P/M1aUCKuF4UUIp.dXjrHyvnE4PerAVJ93bIu4U' WHERE uid = 1;

Update cache

DELETE FROM cache_entity WHERE cid = 'values:user:1';

Saturday, August 19, 2017

To create new user from Drush command

To create new user using Drush use below command from Drush

drush user-create admin --mail="person@example.com"  --password="admin"
Where
Admin is the username
mail is the email address
and Password