Sunday, September 3, 2017

PSR-1 and PSR-2 coding standards for PHP

Visual aspects of code play a significant role in raising or drowning developer's productivity. In case that there's too much clutter, no clear patterns and no lots of randomness in style, code reading can be difficult, and even stressful. This is why developers are constructing ways of dealing with the code complexity. They hope it can make everyday programming life easier. And I'd say they have the point. It really is important to have a clean, flowing, easy to read code.

One good example of such a coding standard is PSR-1, and it's extension PSR-2. Some of the rules of these standards are:


  • Files MUST use only <?php and <?= tags.
  • Files MUST use only UTF-8 without BOM for PHP code.
  • Files SHOULD either declare symbols (classes, functions, constants, etc.) or cause side-effects (e.g. generate output, change .ini settings, etc.) but SHOULD NOT do both.
  • Namespaces and classes MUST follow an "autoloading" PSR: [PSR-0, PSR-4].
  • Class names MUST be declared in StudlyCaps.
  • Class constants MUST be declared in all upper case with underscore separators.
  • Method names MUST be declared in camelCase.



More details on these coding standards can be found here and here.

Thursday, August 17, 2017

WordPress - init hook seems to be fired more than once

init hook should be fired only once in WP

If it appears as being fired more than once, it's probably because there was a redirect and/or 404 error (even for a PNG, it's going to trigger the full WP Load).



Also, there might be multiprocess/multithread execution. Every page load can run multiple wp processes in case of 404 errors or redirects! And they can enter a race condition!

Saturday, August 12, 2017

mysql 5.7 - disabling STRICT mode

If your app was written for older versions of MySQL and is not compatible with strict SQL mode in MySQL 5.7, you can disable strict SQL mode. For example, apps such as WHMCS 6 and Craft 2 do not support strict SQL mode.

If you're using WHMCS 7, see our article on customizing MySQL for WHMCS 7.
To disable strict SQL mode, SSH in to your server as root and create this file:

/etc/mysql/conf.d/disable_strict_mode.cnf
Open the file and enter these two lines:

[mysqld]
sql_mode=IGNORE_SPACE,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Restart MySQL with this command:

sudo service mysql restart
This change disables two SQL mode settings, STRICT_TRANS_TABLES and ONLY_FULL_GROUP_BY, that were added in MySQL 5.7 and cause problems for some older applications.

Full details here

Friday, August 4, 2017

npm, Homestead, and Windows - EIO npm error - solved

If you are using npm inside of a Homestead box which is being hosted in Windows, and you see a "Exit status 1" error , or EIO error thrown by npm scripts, make sure that you have downloaded the npm packages inside of Homestead box, and not in Windows.

Apparently, there could be some differences in encoding / character set which can result in npm errors inside of Homestead box.

So, inside your Homestead box, remove the node_modules file and re-run npm install. Now you should be able to run npm scripts normally.

Tuesday, August 1, 2017

Laravel Cashier - There are no commands defined in the "cashier" namespace. [Solved]

In Laravel 5.4, when trying to use Laravel\Cashier, the following error can sometimes occur:
There are no commands defined in the "cashier" namespace.

The solution is to manually create the migration file and execute it:

Run this:
php artisan make:migration add_cashier_table_fields
And then, in the created empty file, add this:

[code]

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddCashierTableFields extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        //

         Schema::table('users', function ($table) {
            $table->string('stripe_id')->nullable();
            $table->string('card_brand')->nullable();
            $table->string('card_last_four')->nullable();
            $table->timestamp('trial_ends_at')->nullable();
        });
        Schema::create('subscriptions', function ($table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->string('name');
            $table->string('stripe_id');
            $table->string('stripe_plan');
            $table->integer('quantity');
            $table->timestamp('trial_ends_at')->nullable();
            $table->timestamp('ends_at')->nullable();
            $table->timestamps();
        });
     
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}


[/code]

Now, you can execute the command:
php artisan migrate


Original solution is here:
http://itsolutionstuff.com/post/how-to-solve-there-are-no-commands-defined-in-the-cashier-namespace-in-laravel-5example.html

Friday, July 28, 2017

Homestead - recreate database after it's name being changed in Homestead.yaml

When a database name is changed in Homestead.yaml, this database is not cretead until you run this command:

homestead reload --provision

Sunday, July 23, 2017

MySQL crash recovery - ibdata1 file

It proved to me working in my case:

If you don't have backup.
I think the recommended way to recover that databases is start MySQL with innodb_force_recovery = 4 (or higher values) and dump the databases to a SQL.Then drop it and recover from backup. Instead of dropping the original database I prefer to create the new databases with another name or in another server and check the content first.
STEPS TO BE FOLLOWED
  1. In mysqld section of my.cnf add a line innodb_force_recovery = 4 and then restart MySQL server using /etc/init.d/mysql restart.
  2. Take backup.
  3. Restore it on other server and verify the contents.
Please also have a look at Forcing InnoDB Recovery.
UPDATE : For your comment how to progress on that so that it would not happen again
You should not kill MySQL it is not a good practice that may cause MySQL server to crash,whenever you make any configuration changes you should proceed as follows.
  1. Safely stop MySQL Server using /etc/init.d/mysql stop
  2. Make changes
  3. Restart MySQL Server

Saturday, July 15, 2017

MSSQL - list all foreign tables for a table

Useful command which returns a list of all referenced (foreign) tables for a MSSQL table:

EXEC sp_fkeys 'TableName'

Sunday, July 9, 2017

ReactJS - If componentWillReceiveProps is not firing, double check the syntax of mapStateToProps

In ReactJS, the first obvious reason for componentWillReceiveProps() not firing is the use of mutable data. However, if that's not the case, you should always double check the form of your mapStateToProps() function.

Here, the first example works as expected, but the second one contains a logical, but not syntax error which can be a problem to find sometimes.

EXAMPLE:

CORRECT:

var mapStateToProps=(state)=>({

    curOrder:state.orderEditorReducer.curOrder,


});



INCORRECT (not returning anything, just a function declaration):


var mapStateToProps=(state)=>function(){

  return {

    curOrder:state.orderEditorReducer.curOrder,


  }

};

Friday, July 7, 2017

[solved] - PermError SPF Permanent Error: Unknown mechanism found: -all

Super useful article about SPF records which got rejected by mail servers, although they look valid in validators ( it's all about hyphen sign, which looks similar , but it's not the same in validator and at the actual SPF record registry):

https://www.stephendonaghy.com/?p=29

Friday, June 30, 2017

[SOLVED] - npm install crashes with error code 128

Error scenario:

You are trying to run npm install on an existing git repository, but your npm install stops somewhere at fetchMetadata and throws an error code 128, which doesn't tell you much.

First of all, there are multiple possible causes for error code 128, most of them have something to do with permissions, so make sure you have access permissions and you're using proper ssh key(if any).

However, if that doesn't solve the problem, you might try this:
Try to split the package.json file's dependency list into N subgroups and run npm install on those chunks.

This way, it will download some packages every time, and it won't download them anymore when you run npm install for the next chunk.

I'm not sure what's root cause of this bug, but it seems to be a potential bug in npm v.5.0.0+. This needs some more research, but for the moment, I hope that the trick of splitting the npm install load will do the trick for you!


This was tested with node v8.0.0.0 and v8.1.3  with npm v5.0.0 and v5.1.3.
OS: Ubuntu 16.04
(nvm was used as the node manager, and the package.json contained both free and private git repos hosted at bitbucket and github).

UPDATE:
Possible reason for this error is the too high number of connections npm is trying to open. For some internet routers / connections, it might simply be too much. Currently, there's no official ability to limit max connections from npm, even there were some discussions on this in past. Lets hope this will be added into the newer versions.

Also, npm's error log is very bad at reporting the root cause of this error. In most cases, it simply says error code 128. However, every once in a while, it will print out something like Connection refused, or cannot connect to remote repository - host unknown, or something.

This error has a tendency to show up at large repos , filled with lots of git+ssh entries.

This issue might be somewhat related to this problem too:
https://github.com/npm/npm/issues/7862

PSR-1 and PSR-2 coding standards for PHP

Visual aspects of code play a significant role in raising or drowning developer's productivity. In case that there's too much clutte...