PyCharm Auto-import works differently to PHPStorm and IntelliJ

Having become used to developing in JetBrain’s PHPStorm & IntellliJ IDEs it nows seems tedious to break out of the programming flow to manually type out imports every time we introduce a new dependency.

However in that company’s Python IDE, PyCharm, auto-complete works differently. The:

  [ctrl] + [space]

keyboard shortcut still auto-suggests but doesn’t include non-imported classes, but the

  [ctrl] + [alt] + [space]

keyboard shortcut does! Displaying all available classes and auto-generating the import statement for you, just like in JetBrains other IDEs.

Advertisements

Different versions of shell commands like PHP, Vi – find the true location of Mac/Unix commands, aliases and links

Sometimes it can be a challenge to uncover what terminal command is actually being run on Mac / Unix / Linux systems. Over time, old aliases, links and the repeated installation, upgrading and removal of software can lead to existing and even untouched default commands either unable to be found or running a different version than expected.

Recently we have been using Homebrew to swap in and out multiple versions of PHP (5.4, 5.6, 7.1 etc.) on Macs. Whilst great for quickly testing and developing across versions, this makes it possible to have strange occurrences, like the $ which [command] returning a different version of the software to the one which the shell actually executes!

The following location mismatch demonstrates the problem:

# Problem: Command or Software (in this case php)
# Reported as missing or runs at different location


$ php -v
-bash: /usr/local/bin/php: No such file or directory


$ which php
/usr/bin/php


# WTF ???

Different locations are being referenced by $ which [command]  and the shell.

To fix the discrepancy, use the $ hash [command] to refresh the shell’s command location cache:

# Result before hash command
$ command -v
php /usr/local/bin/php

$ hash php
$

# Result after command
$ command -v php
/usr/bin/php

# Same version recognized everywhere
$ which php
/usr/bin/php

$ php -v
PHP 5.5.36 (cli) (built: May 29 2016 01:07:06)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies

Great, order has returned to the universe and which is again pointing at the correct place. Keep in mind if you do replace (or re-link) the php at /usr/local/bin/php it will be executed instead of the /usr/bin/php, but now all commands should agree on the file’s location.

Debugging Aliases

The above situation can be particularly tricky when dealing with aliases.

If we define an alias and confirm it can be executed, the $ which [alias] and ls commands bizarrely return nothing! Where is the alias going? To find where and what the alias is actually running, utilise the $ type -a [command] command:

# Create alias and watch the confusing behavior
$ alias phpv='php -v'
phpv
PHP 5.5.36 (cli) (built: May 29 2016 01:07:06)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies


$ which phpv
$


$ ls -l phpv
ls: phpv: No such file or directory


# The type command reveals all (-a)
$ type -a phpv
phpv is aliased to `php -v'

Debugging Links

Links act slightly different again. As discovered in this blog post, vi links to vim on a mac, however the which, type -a and command -v commands only shows the link name, not the target:

# These commands give information on links (ls -l is most useful)

$ which vi
/usr/bin/vi

$ type -a vi
vi is /usr/bin/vi

$ command -v vi
/usr/bin/vi

$ ls -l /usr/bin/vi
lrwxr-xr-x 1 root wheel 3 21 Aug 15:29 /usr/bin/vi -> vim

Here we can see that although ls -l was useless for aliases and commands, it is good for showing where links point to.

Although only a summary, these quick commands give a few pointers for when a command either cannot be found or a different version than expected is executing. If you’re still struggling to locate an executed command or know of other handy tips, please leave a comment.

Please share if you find our free website useful, it really helps us produce more tutorials!

When mocking a method in PHPUnit produces a “cannot be configured” error

When mocking an object in PHPUnit testing, if you receive the following error even when the function name  is correct. For example, if mocking non-custom Doctrine Repositories (i.e. no SomeEntityRepository class) will cause:

Error:
Trying to configure method “getUser” which cannot be configured because it does not exist, has not been specified, is final, or is static

This occurs on a PHPUnit_Framework_MockObject_MockObject object when using the PHPUnit_Framework_TestCase and PHPUnit_Framework_MockObject_MockBuilder.

In the case of mocking Doctrine find calls, often this error means $methodName parameter in the getMockBuilder($methodName) function is incorrect. For example the following code is incorrect:

// Produces error
$itemRepoMock = $this
  ->getMockBuilder('AppBundle\Entities\ItemRepository')
  ->method('find')
  ->disableOriginalConstructor()
  ->getMock();
$itemRepoMock
  ->expects($this->any())
  ->method('find')
  ->will($this->returnValue($itemMock));

Here you need to specify Doctrine’s base EntityRepository class instead, so:
AppBundle\Entities\ItemRepository
becomes
Doctrine\ORM\EntityRepository
The same example:

// Fixed
$itemRepoMock = $this
  ->getMockBuilder('Doctrine\ORM\EntityRepository')
  ->method('find')
  ->disableOriginalConstructor()
  ->getMock();
$itemRepoMock
  ->expects($this->any())
  ->method('find')
  ->will($this->returnValue($itemMock));

As a defined ItemRepository does not exist, neither dies its find function, even though you can usually use the inferred name.

Fix a Git Commit Message Typo

If after quickly typing in a commit you notice a typo, as such:

git commit -m "this is splet wrong"

You can quickly fix this using Git’s --amend flag, as such:

  1. Type: git commit --amend
  2. By default, the vi editor is opened. press a to enter INSERT mode and make changes
  3. Press ESC to leave INSERT mode and save by typing :wq (write and quit), if you accidently made everything 10x worse, you can cancel by typing :q! (quit, ignoring changes)
  4. Your commit now has an updated commit message and you can push as normal

No more embarrassing errors highlighting non-compliance with the absurd and illogical English (or other) written language.